How to list complete XML document using XSLT

前端 未结 2 847
暖寄归人
暖寄归人 2020-12-21 10:35

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

相关标签:
2条回答
  • 2020-12-21 11:03

    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 讨论(0)
  • 2020-12-21 11:12

    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>
    
    0 讨论(0)
提交回复
热议问题