Global xsl:param containing xpath expression string

Discussion in 'XML' started by kurt hansen, Sep 7, 2004.

  1. kurt hansen

    kurt hansen Guest

    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.
    kurt hansen, Sep 7, 2004
    #1
    1. Advertising


  2. > <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
    David Carlisle, Sep 7, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Christophe Michel

    param containing nodes and for-each

    Jean-Christophe Michel, Jul 12, 2003, in forum: XML
    Replies:
    4
    Views:
    1,800
    Jean-Christophe Michel
    Jul 13, 2003
  2. Geathaa
    Replies:
    2
    Views:
    688
    Geathaa
    Jul 30, 2003
  3. Karsten Weinert
    Replies:
    3
    Views:
    1,002
    Karsten Weinert
    Jul 5, 2004
  4. ectoplasm
    Replies:
    12
    Views:
    635
    Zorro
    Jul 28, 2005
  5. Dave
    Replies:
    5
    Views:
    648
    John Bokma
    Apr 26, 2011
Loading...

Share This Page