Re: [xsl] References to elements in predicates

Subject: Re: [xsl] References to elements in predicates
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Wed, 23 Sep 2009 17:55:15 +0530
It seems, you can avoid a variable with following code:

<xsl:copy-of select="//items/item[name = current()/ref]"/>

(this is untested, please)

On Wed, Sep 23, 2009 at 5:48 PM, rowan@xxxxxxxxxxxxxxxxxxxxx
<rowan@xxxxxxxxxxxxxxxxxxxxx> wrote:
> I'm trying to copy a source document node (including subnodes) which is
> identified by a predicate into my output document. The node to be copied is
> identified because its name element is equal to the ref element of the node
> I'm currently processing.
>
> Here's an example input file:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <items>
> B  <item>
> B  B  B <name>First</name>
> B  B  B <ref/>
> B  B  B <contents>First contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Second</name>
> B  B  B <ref/>
> B  B  B <contents>Second contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Third</name>
> B  B  B <ref/>
> B  B  B <contents>Third contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Fourth</name>
> B  B  B <ref>Second</ref>
> B  B  B <contents>First contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Fifth</name>
> B  B  B <ref/>
> B  B  B <contents>Fifth contents.</contents>
> B  </item>
> </items>
>
> Here's my XSL file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> B  B version="2.0">
> B  B <xsl:output method="xml" indent="yes"/>
>
> B  B <xsl:template match="item[string-length(ref)&gt;0]">
> B  B  B  B <xsl:variable name="ref" select="ref"/>
> B  B  B  B <xsl:copy-of select="//items/item[name=$ref]"/>
> B  B </xsl:template>
>
> B  B <xsl:template match="*">
> B  B  B  B <xsl:copy>
> B  B  B  B  B  B <xsl:apply-templates/>
> B  B  B  B </xsl:copy>
> B  B </xsl:template>
>
> </xsl:stylesheet>
>
> This correctly generates what I'm looking for:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <items>
> B  <item>
> B  B  B <name>First</name>
> B  B  B <ref/>
> B  B  B <contents>First contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Second</name>
> B  B  B <ref/>
> B  B  B <contents>Second contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Third</name>
> B  B  B <ref/>
> B  B  B <contents>Third contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Second</name>
> B  B  B <ref/>
> B  B  B <contents>Second contents.</contents>
> B  </item>
> B  <item>
> B  B  B <name>Fifth</name>
> B  B  B <ref/>
> B  B  B <contents>Fifth contents.</contents>
> B  </item>
> </items>
>
> But why do I need the variable? What do I have to write in the predicate to
> compare the value of the name element in items as I search the file in the
> copy-of with the ref element in the current node? I.e. in this example,
> when I'm processing item 4, the copy-of needs to match the ref element of
> item 4 (which is equal to "Second") with the name of item 2, and return
> item 2's node tree.
>
> Thanks - Rowan



--
Regards,
Mukul Gandhi

Current Thread