xsl-list
[Top] [All Lists]

Re: dynamic XPath?

2003-03-12 14:11:44
"Passin, Tom" <tpassin(_at_)mitretek(_dot_)org> wrote in message
news:5D3C2276FD64424297729EB733ED1F76AF95BB(_at_)email1(_dot_)mitretek(_dot_)org(_dot_)(_dot_)(_dot_)
[Yue Ma]

This could be a dumb question:
Is there a way we can evaluate a dynamic XPath like:
<xsl:if test="$tagPath = 'actual value'">
The tagPath is something like "//root/tag1/tag2"

Seems we can not do it with XSLT 1.0,  right?


Yes we can, depending on how complicated you want the path expression to
be.

This is quite exaggerated. See below.

Here is an example -

XML source -

<root>
<e1>This is e1</e1>
<e2>This is e2</e2>
</root>

=========================

Stylesheet -

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<!-- Path to test against -->
<xsl:variable name='tagPath' select='"/root/e2"'/>

<xsl:template match="/root/*">
<!-- Create a variable to hold a current path expression -->
<!-- Standard expression to create a string for the path of the
current node --> <xsl:variable name='path'>
/<xsl:for-each select="ancestor::*">
   <xsl:value-of select="name()"
/>/</xsl:for-each><xsl:value-of select = "name()" />
</xsl:variable>

What is so "standard" about this expression? It does not uniquely identify
the node -- so it should not qualify as an acceptable solution.



<!-- Test the expression -->
<xsl:if test='normalize-space($path)=$tagPath'>Got path for
<xsl:value-of select='name()'/></xsl:if>
</xsl:template>

</xsl:stylesheet>


This comparison will work in rare cases and will not work in general if
$tagPath evaluates to a single node having preceding siblings.

It will also "work" for more than one node (in case $tagPath evaluates to a
nodeset of two or more nodes) and most probably is not what the author of
the original message wanted.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




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



<Prev in Thread] Current Thread [Next in Thread>