Global xsl:param containing xpath expression string

K

kurt hansen

hi

I thought that this would be easy, but maybe not so much.

I want to:
pass an xpath expression and a string value to a stylesheet
and
copy the source xml document, changing the value of the node described
by the xpath expression, to the new value.

It seems like it should look something like:

<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:param name="xpath-string"/>
<xsl:param name="new-value"/>

<xsl:template match="/|*">
<xsl:copy>
<xsl:choose>
<xsl:when test="is current node the desired node?">
<xsl:call-template name="update-selected-node"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:apply-templates select="@*|node()"/>
</xsl:eek:therwise>
<xsl:choose>
</xsl:copy>
</xsl:template>

<xsl:template match="@*|comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template name="update-selected-node">
<xsl:value-of select="new-value"/>
</xsl:template>

</xsl:stylesheet>


How do you select or test for the node specified by $xpath-string?
Can you put the $xpath-string into a xsl:template match attribute?

thanks.
 
D

David Carlisle

<xsl:param name="xpath-string"/>

XSLT variables can not be bound to Xpath expressions, only to strings
(and other things) so if you need to evaluate a string as an Xpath at run
time you need to have an Xpath parser and evaluator available at run
time. Some systems provide such an extension function (eg
saxon:evaluate()
but in pure XSLT it isn't really possible. in simple cases, eg you just
want to pass in an element name rather than a full xpath, you can use an
Xpath of the form
//*[name()=$xpath-string]
to select elements with a name passed in as a parameter.
or in the form you have it
replace
<xsl:when test="is current node the desired node?">
by
<xsl:when test="name()=$xpath-string">
would work do long as xpath-string is just an element or attribute
name.


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top