在XSLT select中使用一个变量

时间:2021-12-30 22:38:30

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"

我正在尝试创建一个命名模板或函数,其中传入节点名,它将选择该模板或函数作为xpath表达式的最后一层。但它返回的是我作为一个param传入的字符串。在下面的示例中,返回的值是“name”

XSLT:

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>

INPUT XML:

输入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>

3 个解决方案

#1


4  

change

改变

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

to

<xsl:variable name="x" select="sources/source[@type='C']/*[name()=$field-name]"/>

it returns:

它返回:

Joe

#2


2  

The problem here:

这里的问题:

select="sources/source[@type='C']/$field-name"

is that the variable $field-name contains a string, not a location path - so that the expression expands to:

变量$field-name包含一个字符串,而不是一个位置路径——因此表达式扩展为:

select="sources/source[@type='C']/'name'"

If you're using an XSLT 2.0 processor, then you are likely to have access to an evaluate() function that can convert a string into a path, e.g. http://www.saxonica.com/documentation9.4-demo/html/extensions/functions/evaluate.html Otherwise you'll need to use some other method - for example, the one shown by Joel M. Lamsen in his answer.

如果您使用的是XSLT 2.0处理器,那么您可能会访问一个evaluate()函数,该函数可以将字符串转换为路径,例如http://www.saxonica.com/documentation9.4-demo/html/extensions/functions/evaluate.html,否则您需要使用其他一些方法—例如,Joel M. Lamsen在他的答案中所示的方法。

#3


0  

I assume that your named template want to process the entire document, Then xslt is like this ...

我假设您的命名模板要处理整个文档,那么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>

#1


4  

change

改变

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

to

<xsl:variable name="x" select="sources/source[@type='C']/*[name()=$field-name]"/>

it returns:

它返回:

Joe

#2


2  

The problem here:

这里的问题:

select="sources/source[@type='C']/$field-name"

is that the variable $field-name contains a string, not a location path - so that the expression expands to:

变量$field-name包含一个字符串,而不是一个位置路径——因此表达式扩展为:

select="sources/source[@type='C']/'name'"

If you're using an XSLT 2.0 processor, then you are likely to have access to an evaluate() function that can convert a string into a path, e.g. http://www.saxonica.com/documentation9.4-demo/html/extensions/functions/evaluate.html Otherwise you'll need to use some other method - for example, the one shown by Joel M. Lamsen in his answer.

如果您使用的是XSLT 2.0处理器,那么您可能会访问一个evaluate()函数,该函数可以将字符串转换为路径,例如http://www.saxonica.com/documentation9.4-demo/html/extensions/functions/evaluate.html,否则您需要使用其他一些方法—例如,Joel M. Lamsen在他的答案中所示的方法。

#3


0  

I assume that your named template want to process the entire document, Then xslt is like this ...

我假设您的命名模板要处理整个文档,那么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>