【发布时间】:2014-08-18 03:51:51
【问题描述】:
我正在尝试创建一个命名模板或函数,其中我传递一个节点名称,它将选择它作为 xpath 表达式的最后一级。但它返回的只是我作为参数传入的字符串。在下面的示例中,返回的值为“name”
XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
输入 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>
【问题讨论】: