Re: [xsl] [xsl-list] Storing an XPath fragment in a variable

Subject: Re: [xsl] [xsl-list] Storing an XPath fragment in a variable
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 13 Jul 2005 17:10:48 -0400
Hi Jason,

At 04:18 PM 7/13/2005, you wrote:
I'm using the following partial XPath expression...:

[name()='RED' or name() = 'GREEN' or name() = 'BLUE']

... in several locations in my stylesheet:

<xsl:for-each select="/*/*[1]/*[name()='RED' or name() = 'GREEN' or
name() = 'BLUE']">

<xsl:for-each select="*[name()='RED' or name() = 'GREEN' or name() = 'BLUE']">

This works, but it's exactly the same as select="RED | GREEN | BLUE", so why don't you use that?

*[name()='RED' or name() = 'GREEN' or name() = 'BLUE'] translates as "all child elements whose name is 'RED', or 'GREEN', or 'BLUE'"

RED | GREEN | BLUE translates as "all RED, GREEN or BLUE child elements".

Is it possible to store a fragment of an XPath expression in a
variable or is there another method of eliminating redundancy that I'm
not aware of?

No there isn't, without an evaluation function such as saxon:evaluate that lets you parse and evaluate strings back into as XPath.

On the other hand, this isn't really necessary most of the time, since you can easily bind the set of nodes (whatever RED, GREEN and BLUE child elements you have) to a variable, and reuse that.


<xsl:variable name="brights" select="RED | GREEN | BLUE"/>
<xsl:apply-templates select="$brights"/>

works just fine. So will select="$brights | GREY" if you want to add GREY children to the mix.

Whether you need something more powerful than this we can't see without looking at your actual case; but this is usually plenty powerful enough.


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 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