RE: [xsl] dynamic XPath?

Subject: RE: [xsl] dynamic XPath?
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Wed, 12 Mar 2003 12:43:41 -0500
[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.  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>

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

</xsl:stylesheet>

Cheers,

Tom P

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


Current Thread