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

Re: [xsl] Comparing attributes and setting the values


Subject: Re: [xsl] Comparing attributes and setting the values
From: "varun bhatnagar varun292006@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 19 Aug 2014 08:33:25 -0000

Thanks a lot Michael! :)

I checked with another xslt processor. It worked.
I have added bit of complication here. I am not trying to merge two xml
files on the basis of same condition with the same logic. The first xml
file is same as above:

File1.xml
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>

The second file looks like this
File2.xml
<?xml version="1.0"?>
<Move-Afile>
  <Afile>
    <Item>
      <PackNumber level="12">222</PackNumber>
    </Item>
    <Item>
      <PackNumber level="12">333</PackNumber>
    </Item>
    <Item>
      <PackNumber level="14">444</PackNumber>
    </Item>
    <Item>
      <PackNumber level="15">555</PackNumber>
    </Item>
    <Item>
      <PackNumber level="15">666</PackNumber>
    </Item>
  </Afile>
</Move-Afile>

The xslt rule I have written is:
<?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/@level[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"/>
                <xsl:apply-templates
select="document('File2.xml')/*//Item"/>
            </target>
        </A>
    </xsl:template>
</xsl:stylesheet>


Output should look like this:
<?xml version="1.0"?>
<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>
    <Item>
      <PackNumber>
        <counter level="5"/>
        <PNumber>12</PNumber>
      </PackNumber>
    </Item>
    <Item>
      <PackNumber>
        <counter level="5"/>
        <PNumber>12</PNumber>
      </PackNumber>
    </Item>
    <Item>
      <PackNumber>
        <counter level="5"/>
        <PNumber>14</PNumber>
      </PackNumber>
    </Item>
    <Item>
      <PackNumber>
        <counter level="6"/>
        <PNumber>15</PNumber>
      </PackNumber>
    </Item>
    <Item>
      <PackNumber>
        <counter level="6"/>
        <PNumber>15</PNumber>
      </PackNumber>
    </Item>
  </target>
</A>

But it is not coming this way. It starts from 1 again for the second file.
I want to be sequential.
What can I do for that?

BR,
Varun


On Mon, Aug 18, 2014 at 4:42 PM, Michael MC<ller-Hillebrand mmh@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Hi Varun,
>
> I just copied your source files into the Sandbox of Kernow for Saxon and
> got the result you describe as desired.
>
> So I assume your XSLT processor may not work correctly?
>
> - Michael
>
> Am 18.08.2014 um 15:36 schrieb varun bhatnagar varun292006@xxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>:
>
> 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.
>
>
>
>   XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <-list/2562907> (by
> email <>)


Current Thread
Keywords