xsl-list
[Top] [All Lists]

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

2005-07-13 14:10:48
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.

So

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

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--