2009-01-30 53 views
1

我有以下XMLXML排序

<fields> 
    <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" /> 
    <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" /> 
    <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" /> 
    <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" /> 
    <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" /> 
</fields> 

我想基於「位置」進行排序它

如何能不能做到?

提前致謝!

+0

什麼語言? XSLT? – foson 2009-01-30 17:44:47

+0

使用什麼? XSLT?一個bash腳本?魔法棒? – 2009-01-30 17:45:07

回答

1

這XSLT 1.0轉化排序:

<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="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="fields"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*"/> 
     <xsl:apply-templates> 
      <xsl:sort select="@position" data-type="number"/> 
     </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加:

<fields> 
    <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" /> 
    <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" /> 
    <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" /> 
    <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" /> 
    <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" /> 
</fields> 

產生想要的結果

<fields> 
    <field position="1" tablename="Students" headername="Sex" fieldreference="Sex" orderbydirection="ASC" /> 
    <field position="2" tablename="Students" headername="Last Name" fieldreference="LastName" orderbydirection="ASC" /> 
    <field position="3" tablename="Students" headername="Race" fieldreference="Race" orderbydirection="ASC" /> 
    <field position="4" tablename="Students" headername="First Name" fieldreference="FirstName" orderbydirection="ASC" /> 
    <field position="5" tablename="Students" headername="State" fieldreference="State" orderbydirection="ASC" /> 
</fields>