XSLT numbering help

Techsavy
Techsavy used Ask the Experts™
on
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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
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

Author

Commented:
Hi,

Gertone, what is $max-Item variable returning?
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
the maximum of the existing item numbers

Author

Commented:

Why do we need this template?

<xsl:template match="item[normalize-space(.)]">
        <xsl:copy-of select="."/>
    </xsl:template>
Information Architect
Top Expert 2006
Commented:
wel, depends,
it copies the item elements that do have content
if you have an identity copy template in place, you can ignore that one

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial