Subject: [xsl] References to elements in predicates From: "rowan@xxxxxxxxxxxxxxxxxxxxx" <rowan@xxxxxxxxxxxxxxxxxxxxx> Date: Wed, 23 Sep 2009 08:18:40 -0400 |
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> <item> <name>First</name> <ref/> <contents>First contents.</contents> </item> <item> <name>Second</name> <ref/> <contents>Second contents.</contents> </item> <item> <name>Third</name> <ref/> <contents>Third contents.</contents> </item> <item> <name>Fourth</name> <ref>Second</ref> <contents>First contents.</contents> </item> <item> <name>Fifth</name> <ref/> <contents>Fifth contents.</contents> </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" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="item[string-length(ref)>0]"> <xsl:variable name="ref" select="ref"/> <xsl:copy-of select="//items/item[name=$ref]"/> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet> This correctly generates what I'm looking for: <?xml version="1.0" encoding="UTF-8"?> <items> <item> <name>First</name> <ref/> <contents>First contents.</contents> </item> <item> <name>Second</name> <ref/> <contents>Second contents.</contents> </item> <item> <name>Third</name> <ref/> <contents>Third contents.</contents> </item> <item> <name>Second</name> <ref/> <contents>Second contents.</contents> </item> <item> <name>Fifth</name> <ref/> <contents>Fifth contents.</contents> </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 -------------------------------------------------------------------- myhosting.com - Premium Microsoft. Windows. and Linux web and application hosting - http://link.myhosting.com/myhosting
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] grouping with xslt 1.0, Martin Honnen | Thread | Re: [xsl] References to elements in, Mukul Gandhi |
Re: [xsl] grouping with xslt 1.0, Martin Honnen | Date | Re: [xsl] References to elements in, Mukul Gandhi |
Month |