Link to home
Start Free TrialLog in
Avatar of Techsavy
Techsavy

asked on

XSLT numbering help

Hi,

I have following XML where some nodes have a number assigned to them. What I am trying to achieve is put some logic to assign a number to the remaining nodes. Which means i want to assign 4 to the node after that has number 3. Can anyone help how to achieve this?

<item>1<item>
<item>2<item>
<item>3<item>
<item><item>
<Item><item>
<item><item>

Thanks,
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

will likely do what you want (XSLT1, XSLT2 could be more elegant
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0">
    <xsl:variable name="max-item">
        <xsl:for-each select="//item[normalize-space(.)]">
            <xsl:sort order="ascending" data-type="number" select="."/>
            <xsl:if test="position() = last()">
                <xsl:value-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <xsl:template match="item[normalize-space(.)]">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="item[not(normalize-space(.))]">
        <xsl:copy>
            <xsl:value-of select="count(preceding-sibling::item[not(normalize-space(.))]) + 1 + $max-item"/>
        </xsl:copy>
     </xsl:template>
</xsl:stylesheet>

Open in new window

Avatar of Techsavy
Techsavy

ASKER

Hi,

Gertone, what is $max-Item variable returning?
the maximum of the existing item numbers

Why do we need this template?

<xsl:template match="item[normalize-space(.)]">
        <xsl:copy-of select="."/>
    </xsl:template>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial