如何使用 xslt 转换 xml?

How to convert xml using xslt?

输入XML文件:

<a>
  <Item key="1">
    <c1>
      <d11>
      </d11>
      <d12 value="1" />
      <d13 />
    </c1>
  </Item>

  <b2>
    <Item key="fix">
      <d21>
      </d21>
      <d22 value="yes" />
      <d23 />
    </Item>
  </b2>

  <b3>
    <c3>
      <d31>
      </d31>
      <Item key="price">
        <e2 value="no" />
        <e3 />
      </Item>
    </c3>
  </b3>
</a>

如何编写 .xsl 样式表以使输出如下所示:

a/Item [@key='1']/c1/d12/@value
a/b2/Item [@key='fix']/d22/@value
a/b3/с2/Item[@key='price']/e2/@value

也就是说,具有@value 属性的标签的完整路径可以包含具有特殊键属性值的 Item 标签。

来点简单的怎么样:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:for-each select="//@value">
        <xsl:for-each select="ancestor::*">
            <xsl:value-of select="name()"/>
            <xsl:if test="@key">
                <xsl:text>[@key="</xsl:text>
                <xsl:value-of select="@key"/>
                <xsl:text>"]</xsl:text>
            </xsl:if>
            <xsl:text>/</xsl:text>
        </xsl:for-each>
        <xsl:text>@value&#10;</xsl:text>
    </xsl:for-each>
</xsl:template> 

</xsl:stylesheet>