Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 9th, 2007, 07:04 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem accessing nodes using document() and id()

I have 2 xml documents, named dynamic.xml and static.xml. The static document is by far larger than dynamic, as it contains static content that is sparsely populated with variables that will be drawn in from the file dynamic, which will be written by the program that will wrap the transform.

say dynamic looks like this
<?xml version="1.0"?>
<!DOCTYPE variables [
<!ELEMENT variables (variable)*>
<!ELEMENT variable (#PCDATA)>
<!ATTLIST variable id ID #REQUIRED>
]>
<variables>
  <variable id="studyYears">2003-2007</variable>
  <variable id="totalADA">3451</variable>
</variables>

static would contain parts like this:
...
<text embedsVars="true">
  Blah blah blah. The years
  <variable name="studyYears" />
  are going to be really swell, since you will receive a total of
  <variable name="totalADA" />
  . Whoopy!
</text>
...

the stylesheet will apply-templates to <text> and <variables> in static. My problem is trying to get the variables from dynamic into the static.xml variable templates.

I was using this as a template for variable

<xsl:template match="variable">
    <xsl:value-of select="id(document('dynamic.xml')/*/@name)" />
</xsl:template>

This works to pull text straight from the document, but I cannot access nodes based on their ids. Can this be done? If so, what am I doing wrong?



lvnrvltn
si phi
 
Old April 9th, 2007, 07:12 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh,

Im using windows xp, and xalan-java 2.7.0 and apache FOP 0.93.

lvnrvltn
si phi
 
Old April 10th, 2007, 03:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Remember that id() searches the document that contains the context node.

When you've got two documents like this it's always a good idea to declare global variables:

<xsl:variable name="staticDoc" select="/"/>
<xsl:variable name="dynamicDoc" select="document('dynamic.xml')"/>


Assume that the context node is the <variable> element in the static doc.

In XSLT 2.0 you can do

<xsl:value-of select="id(@name, $dynamicDoc)"/>

In 1.0 it has to be:

<xsl:variable name="id" select="@name"/>
<xsl:for-each select="$dynamicDoc">
  <xsl:value-of select="id($id)"/>
</xsl:for-each>

The for-each here serves no purpose other than to change the context node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 12th, 2007, 11:05 PM
Registered User
 
Join Date: Apr 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

works great. thanks

lvnrvltn
si phi





Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing nodes contained in a variable geoGregory3 XSLT 2 March 12th, 2008 02:57 PM
Accessing higher level nodes from a template mikeymikey XSLT 3 January 3rd, 2008 09:14 PM
Help accessing specific nodes jshl_wiz Javascript 2 June 29th, 2005 09:45 PM
Copying XML nodes from one document to another hughcr C# 2 May 12th, 2005 01:20 AM
Add and Remove Nodes in XML document. tutul128 XML 3 March 1st, 2004 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.