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

XSL&XPath-选择属性的元素名称

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

    我有一个相当具体的问题也许。。

    //*[@color='red']/@*
    

    我可以精确地显示表达式要求的内容(所有属性),但是我想添加一些有用的信息,比如属性所属的元素名称。

    //*[@color='red']/@* | //*[@color='red']
    

    希望 要使用此XSLT:

    <xsl:param name="built_expression" select="//*[@color='red']/@* />
    <xsl:template match="/">
        <html>
            <body>
                <table>
                    <tr>
                        <th>Element Name</th>
                        <th>Element Content</th>
                    </tr>
    
                    <xsl:apply-templates select="$built_expression"/>
                </table>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="@*|node()">
        <tr>
          <td>
            <xsl:value-of select="ELEMENT name()" />
          </td>
          <td>
            <xsl:value-of select="node()" />
          </td>
        </tr>
    
        ... other display stuff to do with the attributes ...
    </xsl:template>
    

    这显然行不通。。。但我希望你明白我的意思。。

    <xsl:value-of select="name()"/>
    

    只返回属性的名称,但我需要选择属性元素的名称。

    谢谢!如果我需要澄清什么,请告诉我!

    2 回复  |  直到 14 年前
        1
  •  1
  •   user357812 user357812    14 年前

    name(..)
    

    如果上下文节点是选定的属性节点,则此操作有效。

        2
  •  2
  •   Dimitre Novatchev    14 年前
    <xsl:value-of select="name()"/>
    

    只返回 属性元素的名称。

    属性所属的元素被视为其父元素,因此:

    ..

    当上下文节点作为属性发出时,选择该属性所属的元素。

    要查找此元素的名称,只需使用XPath name() 功能:

    name(..)

    当使用作为属性的上下文节点发出时,其计算结果为字符串,该字符串是保存该属性的元素的名称。