[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: Fri, 22 Aug 2014 08:52:34 -0000

Hi Wendell,

Thanks a ton for replying!
I have posted the details in the above mails. But I can give you the
details. I am trying to merge 2 xml files which is having "level"
attribute. The condition is, if the level attribute is equal in the same
xml file then it should set the same level for that element. But if the
level attribute is having different value then it should print the next
value in sequential order.

File1.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:attribute name="aalevel">
        <xsl:value-of
select="count(preceding::PackNumber/@level[not(preceding::PackNumber/@level=
.)])+1"/>
        </xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
        <xsl:attribute name="aalevel">
        <xsl:value-of
select="count(preceding::PackNumber/@level[not(preceding::PackNumber/@level=
.)])"/>
        </xsl:attribute>
        </xsl:otherwise>
        </xsl:choose>
        </xsl:template>


    <xsl:template match="/">
        <xsl:copy>
        <xsl:apply-templates select="//PackNumber"/>
                <xsl:apply-templates
select="document('File2.xml')/*//PackNumber"/>
</xsl:copy>
    </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="6"/>
        <PNumber>14</PNumber>
      </PackNumber>
    </Item>
    <Item>
      <PackNumber>
        <counter level="7"/>
        <PNumber>15</PNumber>
      </PackNumber>
    </Item>
    <Item>
      <PackNumber>
        <counter level="7"/>
        <PNumber>15</PNumber>
      </PackNumber>
    </Item>
  </target>
</A>


The xsl rules written by me gives me the value of levels as 1, 1, 2, 3, 4,
1, 1, 2, 3, 3. But the actual result should be 1, 1, 2, 3, 4, 5, 5, 6, 7, 7
(as given in output.xml).
I was trying to copy all Item elements and then increase the counter. But
that is not working

Thanks,
BR,
Varun



On Thu, Aug 21, 2014 at 8:34 PM, Wendell Piez wapiez@xxxxxxxxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Varun,
>
> It is impossible to help without more information from you.
>
> Do you have any templates matching "Item"? If so, what do they say?
>
> Do we know for a fact that your templates are matching successfully?
> What kind of output are you getting and how is it not yet correct?
>
> Regards, Wendell
>
>
> On Thu, Aug 21, 2014 at 5:28 AM, varun bhatnagar varun292006@xxxxxxxxx
> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> > Hi,
> > To join all the Item elements I tried using copy and copy-to elements of
> xsl
> > and then I applied the template rules but it did not work.
> >
> >  <xsl:template match="/">
> >          <A>
> >             <target>
> > <xsl:copy>
> >                  <xsl:apply-templates select="//Item"/>
> >                  <xsl:apply-templates
> > select="document('File2.xml')/*//Item"/>
> > </xsl:copy>
> >              </target>
> >          </A>
> >      </xsl:template>
> >
> > Is this the correct way of doing it? Could anyone please help, I am not
> able
> > to proceed :(
> >
> > Thanks,
> > BR,
> > Varun
> >
> >
> > On Tue, Aug 19, 2014 at 1:11 PM, varun bhatnagar varun292006@xxxxxxxxx
> > <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> >>
> >> I am sorry I am bit new to xslt but isn't the / template joining all the
> >> Item elements?
> >>
> >>  <xsl:template match="/">
> >>          <A>
> >>             <target>
> >>                  <xsl:apply-templates select="//Item"/>
> >>                  <xsl:apply-templates
> >> select="document('File2.xml')/*//Item"/>
> >>              </target>
> >>          </A>
> >>      </xsl:template>
> >>  </xsl:stylesheet>
> >>
> >> Regards,
> >> Varun
> >>
> >>
> >> On Tue, Aug 19, 2014 at 11:43 AM, Michael MC<ller-Hillebrand
> mmh@xxxxxxxxx
> >> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> >>>
> >>> Am 19.08.2014 um 10:33 schrieb varun bhatnagar:
> >>>
> >>> > <xsl:template match="/">
> >>> >         <A>
> >>> >             <target>
> >>> >                 <xsl:apply-templates select="//Item"/>
> >>> >                 <xsl:apply-templates
> >>> > select="document('File2.xml')/*//Item"/>
> >>> >             </target>
> >>> >         </A>
> >>> >     </xsl:template>
> >>> > </xsl:stylesheet>
> >>>
> >>> > 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?
> >>>
> >>> Without even understanding what your codebs logic is trying to do
this
> is
> >>> what I would expect. I would not expect the preceding:: axis to
> >>> automagically work across any number of sequential xsl:apply-templates.
> >>>
> >>> It seems to me you must use a two-step approach by first joining all
> >>> <Item> into a single root element.
> >>>
> >>> - Michael
> >>>
> >>
> >> XSL-List info and archive
> >> EasyUnsubscribe (by email)
> >
> >
> > XSL-List info and archive
> > EasyUnsubscribe (by email)
>
>
>
> --
> Wendell Piez | http://www.wendellpiez.com
> XML | XSLT | electronic publishing
> Eat Your Vegetables
> _____oo_________o_o___ooooo____ooooooo_^


Current Thread
Keywords