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

Subject: Re: [xsl] Selecting the value from diff i/p XML
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 16 Sep 2010 06:14:25 +0530
Hi Shashank,
   Here are two solutions both for XSLT 1.0 and 2.0 (untested):

1.0

(this is a very naive solution using a simple lookup by the XPath '='
operator, and a linear for loop)

<xsl:variable name="doc2" select="document('Root2.xml')" />

<xsl:template match="root1">
   <xsl:for-each select="item[@id = $doc2/root2/data/@id]">
       <xsl:value-of select="@value" />
       <br/>
   </xsl:for-each>
</xsl:template>


2.0

<xsl:key name="by-id" match="root2/data" use="@id" />

<xsl:variable name="doc2" select="document('Root2.xml')" />

<xsl:template match="root1">
   <xsl:for-each select="item[@id = key('by-id', current()/@id, $doc2)/@id]">
      <xsl:value-of select="@value" />
      <br/>
   </xsl:for-each>
</xsl:template>

This defines a key, and uses the 3 argument XSLT 2.0 key() function,
which allows us to use another document (than the primary XML
document) as key lookup source, and as known key makes the lookups
faster (the indexing benefit), if search space is large.

On Thu, Sep 16, 2010 at 2:51 AM, Shashank Jain <shashankjain@xxxxxxxx> wrote:
>
> Hello All,
>
> I have two XML docs
> Root1.xml and Root2.xml
>
> ==============================
> Root1.xml looks like this
> <root1>
> B B B  B B B  <item id="1" value="A"/>
> B B B  B B B  <item id="21" value="B"/>
> B B B  B B B  <item id="3" value="C"/>
> B B B  B B B  <item id="45" value="D"/>
> B B B  B B B  <item id="5" value="E"/>
> B B B  B B B  <item id="6" value="F"/>
> B B B  B B B  <item id="78" value="G"/>
> B B B  B B B  <item id="8" value="H"/>
> B B B  B B B  <item id="9" value="I"/>
> B B B  B B B  <item id="10" value="J"/>
> </root1>
>
> ==================================
> Root2.XML looks like this
>
> <root2>
> B B B  <data id="1"/>
> B B B  <data id="2"/>
> B B B  <data id="3"/>
> B B B  <data id="4"/>
> B B B  <data id="5"/>
> B B B  <data id="6"/>
> B B B  <data id="7"/>
> B B B  <data id="8"/>
> B B B  <data id="9"/>
> B B B  <data id="10"/>
> B B B  <data id="11"/>
> B B B  <data id="2"/>
> B </root2>
> ===================================
>
> I want the compare the @id of both the <item> and <data> and when it matches
I want the value of the @value.
> so for in this case my output would be
>
> A
> C
> E
> F
> H
> I
> J
> ========================================
> I wrote this XSLT
>
> <xsl:key name="idlist" match="root2/data" use="@id"/>
> <xsl:variable name="data" select="document('Root1.xml')"/>
> <xsl:template match="/">
> B B B B  <xsl:for-each select="key('idlist', $data/root1/item/@id)">
> B B B B  <xsl:value-of select="@id"/>
> B B B B  <br/>
> B B B B  </xsl:for-each>
> </xsl:template>
>
> and
> I am getting this output
>
> 1
> 3
> 5
> 6
> 8
> 9
> 10
> By applying this XSLT on Root2.xml I am able to print the @id of <data>
element. But how to get the @value of <item> element from Root1.xml.
> I always want to apply my XSLT on Root2.xml
>
> Thanks,
> Shashank jain



--
Regards,
Mukul Gandhi

Current Thread