使用XSLT对XML元素进行排序

时间:2014-04-24 18:00:07

标签: xslt-2.0

我正在进行XML转换,在开始转换XML之前,我只对子元素进行排序。我当前的XSL文件整理了子元素,甚至是我不想排序的父元素

请参阅下文

我的XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Catalog>
    <libraries>
        <library3>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library3>
        <library1>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library1>
        <library4>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library4>
        <library2>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library2>
    </libraries>
    <Books>
        <Book1>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book1>
        <Book3>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book3>
        <Book6>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book6>
        <Book2>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book2>
    </Books>

</Catalog>

欲望输出

<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
  <libraries>
    <library1>
      <city> Marietta </city>
      <Name> COBB </Name>
    </library1>
    <library2>
     <city> Marietta </city>
     <Name> COBB </Name>
    </library2>
    <library3>
     <city> Marietta </city>
     <Name> COBB </Name>
    </library3>
    <library4>
     <city> Marietta </city>
     <Name> COBB </Name>
    </library4>
   </libraries>

   <Books>
    <Book1>
     <author>Great Expectations</author>
     <Name>Wise Otherwise</Name>
    </Book1>
    <Book2>
     <author>Great Expectations</author>
     <Name>Wise Otherwise</Name>
    </Book2>
    <Book3>
     <author>Great Expectations</author>
     <Name>Wise Otherwise</Name>
    </Book3>
    <Book6>
      <author>Great Expectations</author>
      <Name>Wise Otherwise</Name>
     </Book6>
   </Books>
   </Catalog>

我的XSL

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Catalog/Books">
    <xsl:copy>
         <xsl:apply-templates select="node()">
            <xsl:sort select="name()"/>
         </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="Catalog/libraries">
    <xsl:copy>
         <xsl:apply-templates select="node()">
            <xsl:sort select="name()"/>
         </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

<xsl:template match="node()|@*">
<xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Catalog/Books">
<xsl:copy>
     <xsl:apply-templates select="node()">
        <xsl:sort select="name()"/>
     </xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="Catalog/libraries">
<xsl:copy>
     <xsl:apply-templates select="node()">
        <xsl:sort select="name()"/>
     </xsl:apply-templates>
</xsl:copy>
</xsl:template>