开发者

How to list complete XML document using XSLT

开发者 https://www.devze.com 2023-03-01 23:04 出处:网络
What i\'m looking for migh开发者_如何学JAVAt seem pretty easy to understand, but i\'m having a hard time making it possible.

What i'm looking for migh开发者_如何学JAVAt 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.


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>


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
0

精彩评论

暂无评论...
验证码 换一张
取 消