tags:

views:

71

answers:

2

I have a constant and a variable that I wann mouch together to select a specific node, this is what I want to do:

<xsl:attribute name="value">
 <xsl:value-of>
  <xsl:attribute name="select">
   <xsl:text>/root/meta/url_params/
   <xsl:value-of select="$inputid" />
  </xsl:attribute>
 </xsl:value-of>
</xsl:attribute>

How come it doesn't work, and what could I do instad?

+4  A: 

There is no runtime evaluation for XPath expression in standar XSLT 1.0

So, depending what is $inputid, you could have different solutions.

But this /root/meta/url_params/$inputid is wrong because right hand of / must be a relative path in XPath 1.0 (in XPath 2.0 can be a function call, also).

For this particulary case you can use:

/root/meta/url_params/*[name()=$inputid]

or

/root/meta/url_params/*[@id=$inputid]

For a general case, I will go with walker pattern like Dimitre's answer.

Alejandro
$inputid in this case equals something like "car" or "passenger" etc.How should I solve it then?
Kristoffer Nolgren
okay, awesome! I love this place!
Kristoffer Nolgren
+4  A: 

While @Alejandro is right that in the general case dynamic evaluation will be needed (and this may be provided in XSLT 2.1+), there are manageable simpler cases.

For example, if $inputid contains just a name, you probably want this:

<xsl:value-of select="/root/meta/url_params/*[name()=$inputid]"/>

We can implement a rather general dynamic XPath evaluator if we only restrict each location path to be an element name:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:param name="inputId" select="'param/yyy/value'"/>

 <xsl:variable name="vXpathExpression"
  select="concat('root/meta/url_params/', $inputId)"/>

 <xsl:template match="/">
  <xsl:value-of select="$vXpathExpression"/>: <xsl:text/>

  <xsl:call-template name="getNodeValue">
    <xsl:with-param name="pExpression"
         select="$vXpathExpression"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="getNodeValue">
   <xsl:param name="pExpression"/>
   <xsl:param name="pCurrentNode" select="."/>

   <xsl:choose>
    <xsl:when test="not(contains($pExpression, '/'))">
      <xsl:value-of select="$pCurrentNode/*[name()=$pExpression]"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="getNodeValue">
        <xsl:with-param name="pExpression"
          select="substring-after($pExpression, '/')"/>
        <xsl:with-param name="pCurrentNode" select=
        "$pCurrentNode/*[name()=substring-before($pExpression, '/')]"/>
      </xsl:call-template>
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on this XML document:

<root>
  <meta>
    <url_params>
      <param>
        <xxx>
          <value>5</value>
        </xxx>
      </param>
      <param>
        <yyy>
          <value>8</value>
        </yyy>
      </param>
    </url_params>
  </meta>
</root>

the wanted, correct result is produced:

root/meta/url_params/param/yyy/value: 8
Dimitre Novatchev
Should be `name()=$inputid`, without the quotes.
Max Toro
@Max-Toro: Thanks for noticing this. FIxed now.
Dimitre Novatchev
Awesome! what would "name()=$inputid" be reffered to, is there a name for that syntax if I wanted to read up on it?
Kristoffer Nolgren
@Kristoffer-Nolgren: whatever is inside these brackets `[...]` is known as `the predicate`. Defined here: http://www.w3.org/TR/xpath/#predicates . The predicate may contain any XPath expression; it is evaluated and if the result isn't of type boolean, it is converted to boolean value.
Dimitre Novatchev
@Dimitre: +1 Great solution! I like this "walker" pattern.
Alejandro