基于节点值重新排序XML

时间:2016-01-11 08:12:03

标签: xml sorting

我有一个需要根据学生ID排序的XML,请提出有效的方法。

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <StudentData xmlns="http://www.example.org/consolidated">
 <Student>
 <StudentID>1</StudentID>
 <StudentBranch>CSE</StudentBranch>
 </Student>
 <Student>
 <StudentID>5</StudentID>
 <StudentBranch>ECE</StudentBranch>
 </Student>
 <Student>
<StudentID>3</StudentID>
<StudentBranch>MECH</StudentBranch>
 </Student>
 <Student>
<StudentID>7</StudentID>
<StudentBranch>MECH</StudentBranch>
</Student>
</StudentData>

1 个答案:

答案 0 :(得分:1)

你可以在xsl:for-each中使用xsl:sort函数,它应该是安静有效的。如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:for-each select="StudentData/Student">
      <xsl:sort select="StudentID"/>
      <!-- your code here, e.g. create new Student Element-->
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>