h-rai
h-rai

Reputation: 3964

Use parameter value as in select in xslt

I have a parameter named event whose value is say event_val in an XSLT file used for transforming an XML.

I want to get <xsl:value-of select="event_val"/> but the resultant transform is event_val within <TimeCommenced> block.

The code is as follows:

<xsl:param name="event"/><xsl:template match="/">
<Event>
      <TimeCommenced>
        <xsl:variable name="time_commenced" select="$event"/>
        <xsl:value-of select="$time_commenced"/>
      </TimeCommenced>
</Event>

Upvotes: 2

Views: 6301

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116982

The contents of a parameter is a string, not an XPath expression. If you have a parameter named "param" and it contains the string "last_name", then the instruction:

<xsl:value-of select="param"/>

will be evaluated as:

<xsl:value-of select="'last_name'"/>

resulting in the literal string "last_name" being output.

Passing the parameter to a variable makes no difference and is entirely redundant. Try instead something like:

<xsl:value-of select="*[local-name() = $param]"/>

if you want to get the contents of a node named "last_name". Or (preferably, IMHO) find some other (less awkward) way to handle the issue.

Upvotes: 5

Related Questions