RE: [xsl] combining two variables to generate XPATH

Subject: RE: [xsl] combining two variables to generate XPATH
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 17 Dec 2002 15:27:37 -0500
At 03:08 PM 12/17/2002, you wrote:
Is that FAQ outdated?? I know that the following code works just fine:

<xsl:template match="proc">
 <xsl:call-template name="foo">
   <xsl:with-param name="node" select="." />
 </xsl:call-template>
</xsl:template>

<xsl:template name="foo">
 <xsl:param name="node"/>
 <xsl:for-each select="$node/branch/leaf">
   <xsl:value-of select="@id" />
 </xsl:for-each>
</xsl:template>

What is different about a path with a single variable, and a path with two variables?

You are being misled a bit by the syntax -- this can happen easily, given how concise and compact XPath is. But "a path with a single variable" is not resolved in the way you are thinking.


Consider these two variable declarations:

<xsl:variable name="nodeset1" select="//node"/>

<xsl:variable name="nodeset2" select="'//node'"/>

These variables both have values assigned by their declarations, but the resemblance stops there. The first variable is assigned a node set, consisting of all the elements called 'node' in the document. The second variable (despite its misleading name) is assigned a string value, "//node".

In this situation, it's possible to say (in XPath/XSLT 1.0), for example, <xsl:for-each select="$nodeset1"> but not <xsl:for-each select="$nodeset2"/>. This is because in XSLT 1.0, it's not possible to iterate over and process a string, although you can do this with node sets.

You can also say "$nodeset1/*" and get back the children of the nodes in variable nodeset1. But you can't say "*/$nodeset1" even though $nodeset1 is a node set. This is because since it's a node set already, you can't use it as a step in an XPath expression. Again despite appearances, "$nodeset1/*" is really an *operation* performed on $nodeset as an argument, not an XPath "Location Path" of the classic sort.

A string doesn't magically turn into a set of nodes (for that, use an extension function to evaluate the string as XPath). Nor can you treat a nodeset as a string -- at least if the string you want is a string that would work as an XPath to select just that set of nodes. (If you treat a node set as a string, you get the string value of the first node in the set in document order.)

I hope that helps. Once you understand how XPath works and what's a node and what's a string in the data model (and how you can assign either to variables and parameters), XSLT gets a lot easier.

Cheers,
Wendell

Why can't I specify a second variable:

<xsl:template name="foo">
 <xsl:param name="node"/>
 <xsl:param name="branch" select="'someBranch'"/>
 <xsl:for-each select="$node/$branch/leaf">
   <xsl:value-of select="@id" />
 </xsl:for-each>
</xsl:template>



From: Mark Wilgus <mwilgus@xxxxxxxxx>

I believe this is not possible.  XSLT doesn't allow you
to use a variable for an XPath expression.  See number 2 at
the following list:

http://www.dpawson.co.uk/xsl/sect2/nono.html

> -----Original Message-----
> From: bix xslt [mailto:bix_xslt@xxxxxxxxxxx]

> >        <xsl:variable name="branch"
> >select="concat($node,'/',$type,'/item')" />
> >        <xsl:value-of select="$branch" />
> ><!--        <xsl:for-each select="$branch">
>



_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963



XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list


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


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread