RE: [xsl] Selecting the value from diff i/p XML

Subject: RE: [xsl] Selecting the value from diff i/p XML
From: Shashank Jain <shashankjain@xxxxxxxx>
Date: Thu, 16 Sep 2010 16:28:32 -0500
Thanks Gerrit for the new solution.

Yea I messed the xsl:key declaration. Thanks for pointing out.

Now there is slight change, earlier I was printing values only of those ID's
which were in root1.xml.
But now I want to look for the values of for root 2.xml after comparing their
id's from root1.xml.
For better understanding I am giving my i/p xml's again.

===================Root1.xml========================
<root1>
        <item id="1" value="A"/>
        <item id="2" value="B"/>
        <item id="3" value="C"/>
        <item id="4" value="D"/>
        <item id="5" value="E"/>
        <item id="6" value="F"/>
        <item id="7" value="G"/>
        <item id="8" value="H"/>
        <item id="9" value="I"/>
        <item id="10" value="J"/>
</root1>

===================Root2.xml========================
<root2>
    <data id="2"/>
    <data id="2"/>
    <data id="3"/>
    <data id="1"/>
    <data id="5"/>
    <data id="5"/>
    <data id="7"/>
    <data id="8"/>
    <data id="4"/>
    <data id="1"/>
    <data id="1"/>
    <data id="2"/>
</root2>
==================Output==================
2=B
2=B
3=C
1=A
5=E
5=E
7=G
8=H
4=D
1=A
1=A
2=B

================XSLT======================
 <xsl:key name="idlist" match="root2/data" use="@id"/>
 <xsl:variable name="data" select="document('Root1.xml')"
as="document-node(element(root1))"/>
   <xsl:template match="/">
     <wrapper>
       <xsl:apply-templates select="root2/data"/>
     </wrapper>
   </xsl:template>
   <xsl:template match="data">
     <out>
        <xsl:if test="$data/root1/item[@id=current()/@id]">
            <xsl:value-of select="@id"/>
            <xsl:text>=</xsl:text>
            <xsl:value-of
select="$data/root1/item[@id=current()/@id]/@value"/>
            <br/>
         </xsl:if>
      </out>
   </xsl:template>

I am able to get the desired output by using above code but it lacks
efficiency.
How to use the key function to get the desired output.

Shashank


----------------------------------------
> Date: Thu, 16 Sep 2010 21:37:22 +0200
> From: gerrit.imsieke@xxxxxxxxx
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Selecting the value from diff i/p XML
>
>
>
> On 16.09.2010 16:20, Shashank Jain wrote:
> >
> > Thanks Gerrit, Hermann, Bauman and Mukul for helping me out.
> >
> > The 3 argument Key function is really cool, but for some reason I am not
getting any output with that.
> > I am trying to run that in the latest version of XML Spy and also tried
using Saxon9he processor.
>
> Maybe you specified version="1.0" instead of 2.0 in the xsl:stylesheet
> element, or you didn't use the modified xsl:key declaration (indexing
> both item and data elements)?
>
> > Mukul, I think you missed that I want to apply my XSLT on Root2.xml.
> > but thanks for your help :-)
>
> If you really need to process root2.xml and need keys for performance,
> you may like this one:
>
>
>
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="2.0" >
>
>
> method="xml"
> indent="yes"
> />
>
>
> name="idlist"
> match="item"
> use="@id"
> />
>
> name="data"
> select="document('Root1.xml')"
> as="document-node(element(root1))"
> />
>
>
>
>
> select="$data/root1"
> mode="lookup-r1-values">
>
>
>
>
>
>
>
>
>

>
>
>
>
>
> -Gerrit

Current Thread