Xml XSLT:将变量传递给xpath

Xml XSLT:将变量传递给xpath,xml,xslt,Xml,Xslt,在XSLT中,我将模板定义为: <xsl:template name="AA"> <xsl:param name="pre_path" /> <xsl:value-of select="concat($pre_path, 'A/B')" /> </xsl:template> 呼叫模板 <xsl:call-template name="AA"> <xsl:with-param name="p

在XSLT中,我将模板定义为:

<xsl:template name="AA">
        <xsl:param name="pre_path" />
        <xsl:value-of select="concat($pre_path, 'A/B')" />
</xsl:template>
呼叫模板

<xsl:call-template name="AA">
    <xsl:with-param name="pre_path" select="'C/'"/>
</xsl:call-template>
xml:

为什么结果输出是C/A/B,而不是Hello world

我期望:

<xsl:value-of select="C/A/B" />
去打个招呼


提前感谢,

下面是几个选项,介绍如何实现动态XPath来选择匹配元素的值

XSLT1.0样式表,它使用递归模板生成动态XPath,为文档中的每个元素调用,并与谓词过滤器内的连接路径进行比较

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">      
        <xsl:call-template name="AA">
            <xsl:with-param name="pre_path" select="'C/'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="AA">
        <xsl:param name="pre_path"/>
        <xsl:for-each select="//*">
            <xsl:variable name="dynamic-xpath">
                <xsl:call-template name="make-xpath">
                    <xsl:with-param name="context-item" select="."/>
                </xsl:call-template>
            </xsl:variable>

            <xsl:value-of select="self::*[concat($pre_path,'A/B') = 
                                  $dynamic-xpath]"/>
        </xsl:for-each>    
    </xsl:template>

    <xsl:template name="make-xpath">
        <xsl:param name="context-item"/>
        <xsl:param name="path"/>

        <xsl:variable name="new-path">
            <xsl:value-of select="local-name($context-item)"/>
            <xsl:if test="normalize-space($path)">
                <xsl:value-of select="concat('/',$path)"/>
            </xsl:if>
        </xsl:variable>

        <xsl:choose>
            <xsl:when test="$context-item/parent::*">
                <xsl:call-template name="make-xpath">
                    <xsl:with-param name="context-item" 
                                    select="$context-item/parent::*"/>
                    <xsl:with-param name="path" select="$new-path"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$new-path"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>
利用动态XPath选择项的XSLT 3.0解决方案:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:call-template name="AA">
            <xsl:with-param name="pre_path" select="'C/'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="AA">
        <xsl:param name="pre_path"/>
        <xsl:variable name="items" as="node()*">
            <xsl:evaluate xpath="concat($pre_path,'A/B')"/>
        </xsl:variable>
        <xsl:value-of select="$items"/>
    </xsl:template>

</xsl:stylesheet>

因为您连接了两个字符串:C/和A/B。但是,我希望结果是C/A/B而不是C/B/A。您是否正在尝试构建xpath并对其求值?嗨,Daniel,是的,结果是C/A/B。我希望将其作为xpath来获取Hello World,除非您使用XSLT 3.0,否则没有标准方法将字符串求值为xpath。根据XSLT处理器的不同,可能有一个扩展函数可用。喜欢或者为什么你不后退一点,解释一下你到底想要完成什么,而不是你是如何决定去做的,这是行不通的。我只是问你想要完成什么。如果我想批评你,你就不能把它错当成别的东西。。。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:call-template name="AA">
            <xsl:with-param name="pre_path" select="'C/'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="AA">
        <xsl:param name="pre_path"/>
        <xsl:value-of select="//*[concat($pre_path,'A/B') = 
                       string-join(ancestor-or-self::*/local-name(), '/')]"/>
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:call-template name="AA">
            <xsl:with-param name="pre_path" select="'C/'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="AA">
        <xsl:param name="pre_path"/>
        <xsl:variable name="items" as="node()*">
            <xsl:evaluate xpath="concat($pre_path,'A/B')"/>
        </xsl:variable>
        <xsl:value-of select="$items"/>
    </xsl:template>

</xsl:stylesheet>