2014-10-02 136 views
0

我有許多XML文件。我要求按字母順序排序。我在嘗試,但輸出不符合標準。我對xslt的世界很陌生,所以來自傳奇的幫助表示感謝。提前致謝。請參閱下面的詳細信息。使用XSLT對XML進行排序

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" article-type=""> 
<front> 
... 
</front> 
<body> 
... 
</body> 
<back> 
<ref-list content-type="alphabetical"><title>REFERENCES<!--AQ2--></title> 
<ref id="aj502589bib1"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Cordes</surname><given-names>J.</given-names></name><name><surname>Lazio</surname><given-names>T.</given-names></name><name><surname>McLaughlin</surname><given-names>M.</given-names></name></person-group><year>2004</year><article-title>THE DYNAMIC RADIO SKY</article-title><source>NewAR</source><volume>48</volume><fpage>1459</fpage><lpage>72</lpage><page-range>1459&#x02013;72</page-range><ext-link ext-link-type="bibcode">2004NewAR..48.1459C</ext-link><pub-id pub-id-type="doi">10.1016/j.newar.2004.09.038</pub-id></element-citation></ref> 
<ref id="aj502589bib2"><element-citation publication-type="confproc"><person-group person-group-type="author"><name><surname>Mattmann</surname><given-names>C.</given-names></name><name><surname>Crichton</surname><given-names>D.</given-names></name><name><surname>Medvidivic</surname><given-names>N.</given-names></name><name><surname>Hughes</surname><given-names>J. S.</given-names></name></person-group><year>2006</year><source>A software architecture-based framework for highly distributed and data intensive scientific applications</source><conf-name>Proc. of the 2006 International Conf. on Software Engineering</conf-name><fpage>721</fpage><lpage>30</lpage><page-range>721&#x02013;30</page-range></element-citation></ref> 
<ref id="aj502589bib3"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Wayth</surname><given-names>R.</given-names></name><name><surname>Brisken</surname><given-names>W.</given-names></name><name><surname>Deller</surname><given-names>A.</given-names></name><etal/></person-group><year>2011</year><article-title>V-FASTR: THE VLBA FAST RADIO TRANSIENTS EXPERIMENT</article-title><source>ApJ</source><volume>735</volume><fpage>97</fpage><ext-link ext-link-type="bibcode">2011ApJ...735...97W</ext-link><pub-id pub-id-type="doi">10.1088/0004-637X/735/2/97</pub-id></element-citation></ref> 
<ref id="aj502589bib4"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Thompson</surname><given-names>D.</given-names></name><name><surname>Wagstaff</surname><given-names>K.</given-names></name><name><surname>Brisken</surname><given-names>W.</given-names></name><etal/></person-group><year>2011</year><article-title>DETECTION OF FAST RADIO TRANSIENTS WITH MULTIPLE STATIONS A CASE STUDY USING THE VERY LONG BASELINE ARRAY</article-title><source>ApJ</source><volume>735</volume><fpage>98</fpage><ext-link ext-link-type="bibcode">2011ApJ...735...98T</ext-link><pub-id pub-id-type="doi">10.1088/0004-637X/735/2/98</pub-id></element-citation></ref> 
<ref id="aj502589bib5"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Romney</surname><given-names>J. D.</given-names></name></person-group><year>2010</year><article-title>VERY LONG BASELINE ARRAY OBSERVATIONAL STATUS SUMMARY</article-title><source>NRAO<!--AQ1--></source><comment><ext-link ext-link-type="uri" xlink:href="http://www.vlba.nrao.edu/astro/obstatus/current/obssum.html.">http://www.vlba.nrao.edu/astro/obstatus/current/obssum.html.</ext-link></comment></element-citation></ref> 
<ref id="aj502589bib6"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Wayth</surname><given-names>R.</given-names></name><name><surname>Tingay</surname><given-names>S.</given-names></name><name><surname>Deller</surname><given-names>A.</given-names></name></person-group><year>2012</year><article-title>LIMITS ON THE EVENT RATES OF FAST RADIO TRANSIENTS FROM THE V-FASTR EXPERIMENT</article-title><source>ApJL</source><volume>753</volume><fpage>L36</fpage><ext-link ext-link-type="bibcode">2012ApJ...753L..36W</ext-link><pub-id pub-id-type="doi">10.1088/2041-8205/753/2/L36</pub-id></element-citation></ref> 
</ref-list></back> 
</article> 

輸出所需:使用

<?xml version="1.0" encoding="UTF-8"?> 
<article xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" article-type=""> 
<front> 
... 
</front> 
<body> 
... 
</body> 
<back> 
<ref-list content-type="alphabetical"><title>REFERENCES<!--AQ2--></title> 
<ref id="aj502589bib1"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Cordes</surname><given-names>J.</given-names></name><name><surname>Lazio</surname><given-names>T.</given-names></name><name><surname>McLaughlin</surname><given-names>M.</given-names></name></person-group><year>2004</year><article-title>THE DYNAMIC RADIO SKY</article-title><source>NewAR</source><volume>48</volume><fpage>1459</fpage><lpage>72</lpage><page-range>1459&#x02013;72</page-range><ext-link ext-link-type="bibcode">2004NewAR..48.1459C</ext-link><pub-id pub-id-type="doi">10.1016/j.newar.2004.09.038</pub-id></element-citation></ref> 
<ref id="aj502589bib2"><element-citation publication-type="confproc"><person-group person-group-type="author"><name><surname>Mattmann</surname><given-names>C.</given-names></name><name><surname>Crichton</surname><given-names>D.</given-names></name><name><surname>Medvidivic</surname><given-names>N.</given-names></name><name><surname>Hughes</surname><given-names>J. S.</given-names></name></person-group><year>2006</year><source>A software architecture-based framework for highly distributed and data intensive scientific applications</source><conf-name>Proc. of the 2006 International Conf. on Software Engineering</conf-name><fpage>721</fpage><lpage>30</lpage><page-range>721&#x02013;30</page-range></element-citation></ref> 
<ref id="aj502589bib5"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Romney</surname><given-names>J. D.</given-names></name></person-group><year>2010</year><article-title>VERY LONG BASELINE ARRAY OBSERVATIONAL STATUS SUMMARY</article-title><source>NRAO<!--AQ1--></source><comment><ext-link ext-link-type="uri" xlink:href="http://www.vlba.nrao.edu/astro/obstatus/current/obssum.html.">http://www.vlba.nrao.edu/astro/obstatus/current/obssum.html.</ext-link></comment></element-citation></ref> 
<ref id="aj502589bib4"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Thompson</surname><given-names>D.</given-names></name><name><surname>Wagstaff</surname><given-names>K.</given-names></name><name><surname>Brisken</surname><given-names>W.</given-names></name><etal/></person-group><year>2011</year><article-title>DETECTION OF FAST RADIO TRANSIENTS WITH MULTIPLE STATIONS A CASE STUDY USING THE VERY LONG BASELINE ARRAY</article-title><source>ApJ</source><volume>735</volume><fpage>98</fpage><ext-link ext-link-type="bibcode">2011ApJ...735...98T</ext-link><pub-id pub-id-type="doi">10.1088/0004-637X/735/2/98</pub-id></element-citation></ref> 
<ref id="aj502589bib3"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Wayth</surname><given-names>R.</given-names></name><name><surname>Brisken</surname><given-names>W.</given-names></name><name><surname>Deller</surname><given-names>A.</given-names></name><etal/></person-group><year>2011</year><article-title>V-FASTR: THE VLBA FAST RADIO TRANSIENTS EXPERIMENT</article-title><source>ApJ</source><volume>735</volume><fpage>97</fpage><ext-link ext-link-type="bibcode">2011ApJ...735...97W</ext-link><pub-id pub-id-type="doi">10.1088/0004-637X/735/2/97</pub-id></element-citation></ref> 
<ref id="aj502589bib6"><element-citation publication-type="journal"><person-group person-group-type="author"><name><surname>Wayth</surname><given-names>R.</given-names></name><name><surname>Tingay</surname><given-names>S.</given-names></name><name><surname>Deller</surname><given-names>A.</given-names></name></person-group><year>2012</year><article-title>LIMITS ON THE EVENT RATES OF FAST RADIO TRANSIENTS FROM THE V-FASTR EXPERIMENT</article-title><source>ApJL</source><volume>753</volume><fpage>L36</fpage><ext-link ext-link-type="bibcode">2012ApJ...753L..36W</ext-link><pub-id pub-id-type="doi">10.1088/2041-8205/753/2/L36</pub-id></element-citation></ref> 
</ref-list></back> 
</article> 

XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
<xsl:output method="xml"/> 

<xsl:template match="/"> 
<xsl:copy-of select="*"/> 
</xsl:template> 
<xsl:template match="back/ref-list"> 
    <xsl:for-each select="ref/element-citation/person-group/name[1]/*"> 
     <xsl:sort select="surname[1]/*"/> 
     <xsl:copy-of select="*"/> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

回答

0

試試這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="back/ref-list"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:apply-templates select="title"/> 
      <xsl:for-each select="ref"> 
       <xsl:sort data-type="text"/> 
       <xsl:copy> 
        <xsl:copy-of select="@*"/> 
        <xsl:apply-templates/> 
       </xsl:copy> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:template>  
</xsl:stylesheet>