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

Subject: Re: [xsl] Selecting the value from diff i/p XML
From: "Imsieke, Gerrit, le-tex" <gerrit.imsieke@xxxxxxxxx>
Date: Fri, 17 Sep 2010 00:23:20 +0200
Again with a key to the root1/item elements:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl=""; version="2.0" >

<xsl:output method="text" />

  <xsl:key name="idlist" match="item" use="@id" />
  <xsl:variable name="data" select="document('root1.xml')"
    as="document-node(element(root1))" />

  <xsl:template match="/">
    <xsl:apply-templates select="root2/data" />

  <xsl:template match="data">
    <xsl:value-of select="@id"/>
    <xsl:value-of select="key('idlist', @id, $data)/@value"/>


On 16.09.2010 23:28, Shashank Jain wrote:

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.

         <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"/>

     <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"/>

  <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="/">
        <xsl:apply-templates select="root2/data"/>
    <xsl:template match="data">
         <xsl:if test="$data/root1/item[@id=current()/@id]">
             <xsl:value-of select="@id"/>
             <xsl:value-of select="$data/root1/item[@id=current()/@id]/@value"/>

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.


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:


method="xml" indent="yes" />

name="idlist" match="item" use="@id" />


select="$data/root1" mode="lookup-r1-values">


-- Gerrit Imsieke Geschdftsf|hrer / Managing Director le-tex publishing services GmbH Weissenfelser Str. 84, 04229 Leipzig, Germany Phone +49 341 355356 110, Fax +49 341 355356 510 gerrit.imsieke@xxxxxxxxx,

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschdftsf|hrer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vvckler
Besuchen Sie uns auf der Frankfurter Buchmesse
in Halle 4.2, Stand G446.

Mehr dazu unter

Current Thread