如何使用XSLT列出完整的XML文档

时间:2011-05-02 18:51:53

标签: xslt

我正在寻找的东西看起来很容易理解,但我很难让它变得可能。

我希望能够使用XSLT将XML文件的内容放在HTML页面上。问题是,我想以递归方式列出所有节点名称(不是内容)。此外,我必须考虑这样一个事实,即我无法预测节点的名称。

因此,如果您知道使用XSLT以递归方式列出XML文件中的所有节点的方法,那么感谢您的答案。

2 个答案:

答案 0 :(得分:5)

由于您询问的是节点名称而不是内容,因此您不需要递归执行,这是最简单的方法。

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:for-each select="//node()">
           <xsl:value-of select="name()"/>
        </xsl:for-each>
    </xsl:template>
  </xsl:stylesheet>

答案 1 :(得分:3)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="*">
  <xsl:value-of select="concat(name(), '&#xA;')"/>
  <xsl:apply-templates select="*"/>
 </xsl:template>
</xsl:stylesheet>

应用于任何XML文档(例如此文件)时:

<Catalog name="AccessoriesCatalog">
    <Category Definition="AccessoriesCategory"
    name="1532" id="1532">
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16115" id="16115">
        <ParentCategory>1532</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16116" id="16116">
        <ParentCategory>16115</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16126" id="16126">
        <ParentCategory>16115</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16131" id="16131">
        <ParentCategory>1532</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16132" id="16132">
        <ParentCategory>16131</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16136" id="16136">
        <ParentCategory>16131</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16139" id="16139">
        <ParentCategory>16131</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16144" id="16144">
        <ParentCategory>16131</ParentCategory>
    </Category>
    <Category Definition="AccessoriesCategory"
    name="16195" id="16195">
        <ParentCategory>16131</ParentCategory>
    </Category>
</Catalog>

生成所需的输出(XML文档中所有元素的名称列表):

Catalog
Category
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory
Category
ParentCategory

如果只需要不同的元素名称,那么这个转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kElemByName" match="*" use="name()"/>

 <xsl:template match="
  *[generate-id()
   =
    generate-id(key('kElemByName', name())[1])
   ]">
  <xsl:value-of select="concat(name(), '&#xA;')"/>
  <xsl:apply-templates select="*"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

产生想要的结果

Catalog
Category
ParentCategory