Subject:selecting nodes in another XML document Author:Scott Renner Date:05 Aug 2005 12:39 PM
I'm trying to select an element and its children from another document, based upon the value of an element in my first document. I know I don't have the correct syntax to select the correct items.
Here is a example.
First document
...
<doc>
<device>
<model>1234AB></model>
</device>
</doc>
Output should be:
<doc>
<device>
<model>1234AB></model>
<feature>1234</feature>
<color>Red</color>
</device>
</doc>
Here is what I have come up with:
<xsl:template match="/doc/device">
<xsl:copy-of select="document('ConfigBase.xml',/devices/model[@id='model'])"/>
</xsl:template>
This doesn't work because @id='model' is looking for the literal "model" as the id. How do I reference the current value of "model" in the @id statement?
The output is now
<?xml version="1.0" encoding="UTF-16"?>
1.012/12/04
<feature>1234</feature>
<color>Red</color>
The lines that do not match the template, the values are just being output. How do I get the stylesheet to output the original XML line with the element tags still in place?
<xsl:template match="/">
<root>
<!-- use for-each in case your main.xml contains more then one model -->
<xsl:for-each select="/doc/model">
<xsl:variable name="model" select="."/>
<xsl:copy-of select="document('ConfigBase.xml')/config/device/model[@id=$model]/*"/>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>