代码之家  ›  专栏  ›  技术社区  ›  developer

元素名(XSL/XPath)未返回任何内容

  •  1
  • developer  · 技术社区  · 14 年前

    以下代码来自生成另一个XSL文档的XSL文档。。其中,$expression是动态创建XPath表达式的变量。

    <xsl:template match="/">
        ...
        <xslt:template match="{$expression}">
            <elem key="{name()}">
                <xslt:copy-of select="@*"/>
                <xslt:for-each select="@*">
                    <xslt:sort select="name()"/>
                    <attribute>|<xslt:value-of select="name()"/>|</attribute>
                </xslt:for-each>
            </elem>
        </xslt:template>
        ...
    </xsl:template>
    

    <xsl:template match="//*[@name='Spot']">
        <elem key="">
            <xsl:copy-of select="@*" />
            <xsl:for-each select="@*">
                <xsl:sort select="name()" />
                <attribute>|<xsl:value-of select="name()" />|</attribute>
            </xsl:for-each>
        </elem>
    </xsl:template>
    

    除了@键为空之外,一切都按预期进行。我唯一的问题是显示元素的名称。

    EDIT:为了澄清我实际上在寻找什么,我需要从 <xslt:template match="{$expression}"> .

    2 回复  |  直到 14 年前
        1
  •  1
  •   Dimitre Novatchev    14 年前

    <elem key="{name()}">
    

    你一定要逃出监狱 { } ,这样就不会立即计算AVT。

    { } 原样 是通过按照 XSLT spec :

    “当属性值模板 表达式外的花括号将 用一个大括号代替。”

    因此,更换 :

    <xslt:template match="{$expression}">   
        <elem key="{name()}">   
            <xslt:copy-of select="@*"/>   
            <xslt:for-each select="@*">   
                <xslt:sort select="name()"/>   
                <attribute>|<xslt:value-of select="name()"/>|</attribute>   
            </xslt:for-each>   
        </elem>   
    </xslt:template> 
    

    使用:

    <xslt:template match="{$expression}">   
        <elem key="{{name()}}">   
            <xslt:copy-of select="@*"/>   
            <xslt:for-each select="@*">   
                <xslt:sort select="name()"/>   
                <attribute>|<xslt:value-of select="name()"/>|</attribute>   
            </xslt:for-each>   
        </elem>   
    </xslt:template> 
    

    <xsl:template match="//*[@name='Spot']">       
        <elem key="{name()}">       
            <xsl:copy-of select="@*" />       
            <xsl:for-each select="@*">       
                <xsl:sort select="name()" />       
                <attribute>|<xsl:value-of select="name()" />|</attribute>       
            </xsl:for-each>       
        </elem>       
    </xsl:template> 
    

    注意 :我很惊讶这段代码能正常工作。你应该使用 <xsl:namespace-alias> XSLT指令。

        2
  •  0
  •   Welbog    14 年前

    / 返回包含XML文档的根节点的节点,而不是根节点本身。此节点没有名称。

    我猜你想知道那个节点的子节点的名字。你可以用很多方法。一是你可以改变你的想法 <xsl:template match="/"> <xsl:template match="/root"> root 或者您可以更改 <elem key="{name()}"> <elem key="{name(root)}">

    <xsl:template match="/*"> . 将返回根的父级的所有子级。在这种情况下,总是只有一个根。那你可以用 <元素键=“{name()}”> 正常情况下。