XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Scott RennerSubject: 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>

Second document

<devices>
<model id="1234AB">
<feature>1234</feature>
<color>Red</color>
</model>
</devices>

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?

Thanks!

Scott

Postnext
Ivan PedruzziSubject: selecting nodes in another XML document
Author: Ivan Pedruzzi
Date: 05 Aug 2005 01:51 PM
Scott,

You have to use a variable like in the following code

<xsl:template match="/doc/device">
<xsl:variable name="model" select="model"/>
<xsl:copy-of select="document('ConfigBase.xml',/devices/model[@id=$model])"/>
</xsl:template>

Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Postnext
Scott RennerSubject: selecting nodes in another XML document
Author: Scott Renner
Date: 05 Aug 2005 03:01 PM
That did the trick!

Thanks

Postnext
Scott RennerSubject: selecting nodes in another XML document
Author: Scott Renner
Date: 05 Aug 2005 04:18 PM
Well, I think I'm almost there, but I still have a problem.

If the first file is:
<doc>
<version>1.0</version>
<date>12/12/04</date>
<model>1234AB</model>
</doc>

and the second file is
<devices>
<model id="1234AB>
<feature>1234</feature>
<color>Red</color>
</model>
</devices>

the xsl file is:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1"/>
<xsl:template match="/config/device">
<xsl:variable name="model" select="model"/>
<xsl:copy-of select="document('ConfigBase.xml')/config/device/model[@id=$model]/*"/>
</xsl:template>
</xsl:stylesheet>

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?

Scott

Posttop
Ivan PedruzziSubject: selecting nodes in another XML document
Author: Ivan Pedruzzi
Date: 05 Aug 2005 04:48 PM
Assuming that the main input is main.xml
<doc>
<version>1.0</version>
<date>12/12/04</date>
<model>1234AB</model>
</doc>

and the second document is ConfigBase.xml
<config>
<device>
<model id="1234AB>
<feature>1234</feature>
<color>Red</color>
</model>
</device>
</config>

you should be able to do the following

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-1"/>

<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>

Hoep this helps
Ivan Pedruzzi
Stylus Studio Team

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.