Xml 在XSLT select中使用变量
我正在尝试创建一个命名模板或函数,在其中传入一个节点名,它将选择该节点名作为xpath表达式的最后一级。但它返回的只是作为参数传入的字符串。在下面的示例中,返回的值是“name” XSLT:Xml 在XSLT select中使用变量,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我正在尝试创建一个命名模板或函数,在其中传入一个节点名,它将选择该节点名作为xpath表达式的最后一级。但它返回的只是作为参数传入的字符串。在下面的示例中,返回的值是“name” XSLT: 名称 输入XML: <?xml version="1.0" encoding="UTF-8"?> <sources> <source type='C'> <name>Joe</name> <age&
名称
输入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>
乔
10
做记号
20
我假设您的命名模板想要处理整个文档,那么xslt如下所示
<xsl:output indent="yes"></xsl:output>
<xsl:template name="get-prefered">
<xsl:param name="field-name"/>
<xsl:variable name="vCondition" select="$field-name/name"/>
<xsl:variable name="x" select="$field-name/sources/source[@type='C']/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" select="."></xsl:with-param>
</xsl:call-template>
</xsl:template>
我假设您的命名模板想要处理整个文档,那么xslt是这样的
<xsl:output indent="yes"></xsl:output>
<xsl:template name="get-prefered">
<xsl:param name="field-name"/>
<xsl:variable name="vCondition" select="$field-name/name"/>
<xsl:variable name="x" select="$field-name/sources/source[@type='C']/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" select="."></xsl:with-param>
</xsl:call-template>
</xsl:template>
改变
<xsl:variable name="x" select="sources/source[@type='C']/$field-name"/>
改变
这里的问题是:
select="sources/source[@type='C']/$field-name"
变量$field name
包含字符串,而不是位置路径-因此表达式扩展为:
select="sources/source[@type='C']/'name'"
如果您使用的是XSLT 2.0处理器,那么您可能有权访问evaluate()函数,该函数可以将字符串转换为路径,例如,否则您将需要使用其他方法,例如Joel M.Lamsen在其回答中所示的方法。这里的问题:
select="sources/source[@type='C']/$field-name"
变量$field name
包含字符串,而不是位置路径-因此表达式扩展为:
select="sources/source[@type='C']/'name'"
如果您使用的是XSLT 2.0处理器,那么您可能有权访问evaluate()函数,该函数可以将字符串转换为路径,例如,否则您需要使用其他方法,例如Joel M.Lamsen在其回答中所示的方法。不完全如此。我希望能够在xpath的最后一级进行pasd(在这个简化的示例中,唯一有效的选择是名称和年龄),并返回一个值,但不完全是。我希望能够在xpath的最后一级进行pasd(在这个简化的示例中,唯一有效的选择是名称和年龄),并返回一个值。似乎它需要xslt 3.0[在编写此答案时没有xslt 3.0。问题是:它在xslt 2.0中可用还是仅在xslt 3.0中可用?似乎它需要xslt 3.0[在编写这个答案时没有XSLT 3.0。问题是:它是在XSLT 2.0中可用还是仅在XSLT 3.0中可用?