[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Comparing attributes and setting the values

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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.