【问题标题】:How to list complete XML document using XSLT如何使用 XSLT 列出完整的 XML 文档
【发布时间】:2011-05-02 18:51:53
【问题描述】:

我正在寻找的东西似乎很容易理解,但我很难让它成为可能。

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

因此,如果您知道一种使用 XSLT 递归列出 XML 文件中所有节点的方法,感谢您的回答。

【问题讨论】:

  • 所以,如果我理解正确的话,您希望将 xml 文档的源代码输出到 html 页面上。对吗?
  • 当您说“节点”时,您的意思是“元素”吗?您是否还想从 cmets 或处理指令等中输出任何内容?
  • 问题的答案有很多,与这个没有太大区别。也不清楚您是否想要所有名称或不同的名称。最好发布一个减少的输入样本和确切的期望输出。
  • 好问题,+1。请参阅我对两种不同解决方案(既简短又简单)的回答,这些解决方案会产生您尚未完全指定的两个不同版本(所有元素名称和所有不同的元素名称)。

标签: xslt


【解决方案1】:

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

<?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>

【讨论】:

  • 你知道吗,这看起来很简单,这让我想在接下来的 30 分钟内用头撞墙。想想我刚刚度过了一整天,试图通过递归模板调用和其他任何无意义的东西来解决问题(比如在 foreach 的选择中使用包含路径的字符串变量)。十分感谢。不过有一件事。如果我使用 current() 的值,它会给我每个子节点的内容。有没有办法只获取节点的内容,而不是子节点。再次感谢
  • &lt;xsl:template match="node()"&gt;&lt;xsl:value-of select="name()"/&gt;&lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; 似乎做同样的事情,但更简单:]
  • @mousio:你说得对,谢谢! @Shadowxvii &lt;xsl:value-of select="name()"/&gt; 应该只给你当前节点的名称。
  • 如果你想要 HTML yes 或者 yes 你可以添加标签:
【解决方案2】:

这种转变

<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

【讨论】:

    猜你喜欢
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多