How to list complete XML document using XSLT

2020-02-14 08:27发布

What i'm looking for might seem pretty easy to understand, but i'm having a hard time making it possible.

I want to be able to put the content of an XML file on an HTML page using XSLT. The thing is, i want to list all the nodes name (not the content), recursively. Also, i have to consider the fact that i can't predict what are going to be the names of the nodes.

So if you know a way to recursively list all the nodes in an XML file using XSLT, thanks for the answer.

标签: xslt
2条回答
家丑人穷心不美
2楼-- · 2020-02-14 09:01

Since you asked for the node names and not the content, you don't need to do it recursively, this is the simplest way.

<?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>
查看更多
该账号已被封号
3楼-- · 2020-02-14 09:07

This transformation:

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

when applied on any XML document (such as this):

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

produces the wanted output (a list of the names of all elements in the XML document):

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

In case only the distinct element names are wanted, then this transformation:

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

produces the wanted result:

Catalog
Category
ParentCategory
查看更多
登录 后发表回答