How to list a complete XML document using XSLT

What I'm looking for may seem fairly easy to understand, but it's hard for me to make this possible.

I want me to be able to put the contents of an XML file on an HTML page using XSLT. The thing is, I want to list all the host names (not the content), recursively. In addition, I must take into account the fact that I cannot predict what the host names will be.

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

+3
source share
2 answers

Since you requested the node names, not the content, you do not need to do this recursively, this is the easiest 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>
+5
source

This conversion is :

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

All Articles