Re: [xsl] Using document() and getting back to the original doc

Subject: Re: [xsl] Using document() and getting back to the original doc
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 22 Mar 2004 13:53:02 -0500
Dan,

At 01:12 PM 3/22/2004, you wrote:
More questions related to the stylesheet parameters and using a config file.

I know how to start a style sheet with a new document using the document() function, but how do you get back to the document that was feed in on the command line?

I'm using saxon, and I pass in a configuration file on the command line (so I can have different file names). This has a list of other files to process and I use document() to process them. But how do I refer back to the original configuration document? Is there a way to capture the input file name as a parameter value - without entering it as a parameter value? Is there some other way to open the "original" source document?

The XSLT engine doesn't ever have a file "open". It merely refers to node trees, without caring how they are built (from a file or otherwise). As you know, nodes can be identified using XPath; but XPath is sensitive to context, which leads to your problem, since your context changes when you do something like apply-templates select="document('myfile.xml')"


But nodes and node sets can also be bound to variables. Just as you can say

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

and thereby have a variable bound to the root node of the XPath rendition of the document retrieved by the function, so also can you do

<xsl:variable name="config" select="/"/>

(at the top level), which binds the root node of the "official" source tree (in your case, the config file) to the variable $config.

This is handly, because it allows you, in any context at all, to look at the $config node or any of its descendants. So you might be matching a node from source1.xml, and you can still say things like <xsl:value-of select="$config//color[@file=current()] (which gets the value of the 'color' element descendant of the $config tree whose @file attribute is the same as the value of the current node).

You can also be more specific, declaring bindings like

<xsl:variable name="colors" select="//color"/>

to collect all the <color> elements in your source file and bind them to $colors. Like any other globally-scoped variable in XSLT, $colors will be the same no matter where you use it.

I hope this helps.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread