Use a variable in XSLT select



I am trying to make a named template or function where I an pass in a node name and it will select that as the last level of a xpath expression. But all it returns is the string I pass in as a param. In the below example the value returned is "name"


XSLT:



<xsl:stylesheet version="2.0" xmlns:xsl="http://ift.tt/tCZ8VR">

<xsl:output indent="yes"></xsl:output>

<xsl:template name="get-prefered">
<xsl:param name="field-name"/>

<xsl:variable name="vCondition" select="name"/>
<xsl:variable name="x" select="sources/source[@type='C']/$field-name"/>
<xsl:value-of select="$x"></xsl:value-of>
</xsl:template>

<xsl:template match="/">
<xsl:call-template name="get-prefered">
<xsl:with-param name="field-name">name</xsl:with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>


INPUT XML:



<?xml version="1.0" encoding="UTF-8"?>
<sources>
<source type='C'>
<name>Joe</name>
<age>10</age>
</source>
<source type='B'>
<name>Mark</name>
<age>20</age>
</source>
</sources>

No comments:

Post a Comment