xsl-list
[Top] [All Lists]

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

2010-09-16 17:23:45
Again with a key to the root1/item elements:


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  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>

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

</xsl:stylesheet>


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.

===================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[(_at_)id=current()/@id]">
             <xsl:value-of select="@id"/>
             <xsl:text>=</xsl:text>
             <xsl:value-of 
select="$data/root1/item[(_at_)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(_dot_)imsieke(_at_)le-tex(_dot_)de
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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

--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:
--~--

                                        
--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--
Gerrit Imsieke
Geschäftsfü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(_dot_)imsieke(_at_)le-tex(_dot_)de, http://www.le-tex.de

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

Geschäftsführer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vöckler
----------------------------------------------------------------------
Besuchen Sie uns auf der Frankfurter Buchmesse
in Halle 4.2, Stand G446.

Mehr dazu unter www.le-tex.de/de/buchmesse.html

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--