Using param value in xpath

Discussion in 'XML' started by jose.jeria@gmail.com, Jul 6, 2005.

  1. Guest

    I have the following
    Code:


    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" indent="yes" encoding="UTF-8"/>
    <xsl:param name="myvar"/>

    <xsl:template match="/">
    <html>
    <body>
    <xsl:for-each select="strings/$myvar">
    <p><xsl:value-of select="."/></p>
    </xsl:for-each>

    </body>
    </html>
    </xsl:template>

    </xsl:stylesheet>



    What i want to do is to use the $myvar in the xpath expression. Its
    marked in bold, but that gives me an error. I tried the concat
    function, but that did also didnt work. What am I doing wrong?
     
    , Jul 6, 2005
    #1
    1. Advertising

  2. Joris Gillis Guest

    Hi,

    > What i want to do is to use the $myvar in the xpath expression. Its
    > marked in bold, but that gives me an error. I tried the concat
    > function, but that did also didnt work. What am I doing wrong?


    Variables or parameters cannot be used in XSLT to dynamically make xpath expression.

    You could use an extension function. e.g http://www.exslt.org/dyn/functions/evaluate/index.html

    But I certainly wouldn't to that myself to solve your specific problem. Simple use:

    'strings/*[local-name()=$myvar]'

    or the lightening fast key equivalent:

    <xsl:key name="node" match="strings/*" use="local-name()"/>

    <xsl:template match="/">
    <html>
    <body>
    <xsl:for-each select="key('node',$myvar)">
    <p><xsl:value-of select="."/></p>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>



    regards,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    Spread the wiki (http://www.wikipedia.org)
     
    Joris Gillis, Jul 6, 2005
    #2
    1. Advertising

  3. wrote:


    > <xsl:param name="myvar"/>


    > <xsl:for-each select="strings/$myvar">


    > What i want to do is to use the $myvar in the xpath expression. Its
    > marked in bold, but that gives me an error. I tried the concat
    > function, but that did also didnt work. What am I doing wrong?


    There is no dynamic XPath expression evaluation, what you have is not
    syntactically correct.
    It is hard to tell you what to use instead as I have no idea what you
    want to achieve, you have not provided any information what type of
    value you set the variable to.
    If you have for instance a variable/param of type string with an element
    name and you want to select elements with that name then
    <xsl:for-each select="strings/*[name() = $myvar]">
    might do what you want.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 6, 2005
    #3
  4. Guest

    Thanks Joris and Martin,

    that was exactly what i was looking for.
     
    , Jul 6, 2005
    #4
    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. Alastair Cameron
    Replies:
    1
    Views:
    7,418
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  2. Geathaa
    Replies:
    2
    Views:
    700
    Geathaa
    Jul 30, 2003
  3. kurt hansen
    Replies:
    1
    Views:
    1,044
    David Carlisle
    Sep 7, 2004
  4. ectoplasm
    Replies:
    12
    Views:
    646
    Zorro
    Jul 28, 2005
  5. Dave
    Replies:
    5
    Views:
    676
    John Bokma
    Apr 26, 2011
Loading...

Share This Page