[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Comparing attributes and setting the values


Subject: [xsl] Comparing attributes and setting the values
From: "varun bhatnagar varun292006@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 18 Aug 2014 13:36:04 -0000

Hi,

I have an xml file given below:

File.xml:
<?xml version="1.0"?>
<Move-Afile>
  <Afile>
    <Item>
      <PackNumber level="1">1234</PackNumber>
    </Item>
    <Item>
      <PackNumber level="1">567</PackNumber>
    </Item>
    <Item>
      <PackNumber level="4">5672</PackNumber>
    </Item>
    <Item>
      <PackNumber level="5">126</PackNumber>
    </Item>
    <Item>
      <PackNumber level="7">876</PackNumber>
    </Item>
  </Afile>
</Move-Afile>

I want to transform this into something like this:

Output.xml:
<?xml version="1.0" encoding="UTF-8"?>
<A>
<target>
<Item>
<PackNumber>
<counter level="1"/>
<PNumber>1</PNumber>
</PackNumber>
</Item>
<Item>
<PackNumber>
<counter level="1"/>
<PNumber>1</PNumber>
</PackNumber>
</Item>
<Item>
<PackNumber>
<counter level="2"/>
<PNumber>4</PNumber>
</PackNumber>
</Item>
<Item>
<PackNumber>
<counter level="3"/>
<PNumber>5</PNumber>
</PackNumber>
</Item>
<Item>
<PackNumber>
<counter level="4"/>
<PNumber>7</PNumber>
</PackNumber>
</Item>
</target>
</A>

The condition is, if the level attribute is equal then it should set the
same level. But if the attribute is having different value then it should
print the next value in sequential order.'

I have written the following xsl rules to achieve this but something is
missing in this.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
 <xsl:output indent="yes"/>

        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="text()" />

        <xsl:template match="PackNumber/@level">
        <xsl:choose>
        <xsl:when test="not(preceding::PackNumber/@level =.)">
        <xsl:element name="counter">
        <xsl:attribute name="level">
        <xsl:value-of
select="count(preceding::PackNumber/@level[not(preceding::PackNumber/@level=
.)])+1"/>
        </xsl:attribute>
        </xsl:element>
        </xsl:when>
        <xsl:otherwise>
        <xsl:element name="counter">
        <xsl:attribute name="level">
        <xsl:value-of
select="count(preceding::PackNumber[not(preceding::PackNumber/@level=
.)])"/>
        </xsl:attribute>
        </xsl:element>
        </xsl:otherwise>
        </xsl:choose>

             <PNumber>
                <xsl:value-of select="."/>
            </PNumber>
        </xsl:template>

    <xsl:template match="/">
        <A>
            <target>
                <xsl:apply-templates select="//Item"/>
            </target>
        </A>
    </xsl:template>
</xsl:stylesheet>

This gives me the value of levels as 1, 1, 1, 2, 3. But the actual result
should be 1, 1, 2, 3, 4 (as given in output.xml).
Can anyone tell me what am I missing here. I came across an example on
internet but that also didn't help.

Thanks,
BR,
Varun


Current Thread
Keywords