使用XSLT基于多个子元素对XML进行排序

时间:2015-06-04 17:35:28

标签: xml sorting xslt

我正在尝试基于一些子元素对一些XML(使用XSLT)进行排序,并将结果作为XML返回。我知道这可能不是那么困难,但这是我第一次使用XSLT的经历,它给了我一些麻烦。这是XML:

<root>
  <subject>
    <courseSubjectHeader>
        <subjectCode>B</subjectCode>
        <subjectName>text</subjectName>
        <unit>text</unit>
        <faculty>text</faculty>
    </courseSubjectHeader>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>B 200</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>B 100</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
  </subject>
  <subject>
    <courseSubjectHeader>
        <subjectCode>C</subjectCode>
        <subjectName>text</subjectName>
        <unit>text</unit>
        <faculty>text</faculty>
    </courseSubjectHeader>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>C 300</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>C 100</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
  </subject>
  <subject>
    <courseSubjectHeader>
        <subjectCode>A</subjectCode>
        <subjectName>text</subjectName>
        <unit>text</unit>
        <faculty>text</faculty>
    </courseSubjectHeader>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>A 300</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>A 200</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
  </subject>
</root>

我想通过'subjectCode'子元素对'subject'进行排序,并通过'subjectAndNumber'子元素对每个主题中的所有课程进行排序。因此得到的XML将是......

<root>
  <subject>
    <courseSubjectHeader>
        <subjectCode>A</subjectCode>
        <subjectName>text</subjectName>
        <unit>text</unit>
        <faculty>text</faculty>
    </courseSubjectHeader>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>A 200</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>A 300</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
  </subject>
  <subject>
    <courseSubjectHeader>
        <subjectCode>B</subjectCode>
        <subjectName>text</subjectName>
        <unit>text</unit>
        <faculty>text</faculty>
    </courseSubjectHeader>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>B 100</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>B 200</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
  </subject>
  <subject>
    <courseSubjectHeader>
        <subjectCode>C</subjectCode>
        <subjectName>text</subjectName>
        <unit>text</unit>
        <faculty>text</faculty>
    </courseSubjectHeader>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>C 100</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
    <course>
      <crsLevel>text</crsLevel>
      <subjectAndNumber>C 300</subjectAndNumber>
      <units>3.0</units>
      <hours>3-0</hours>
    </course>
  </subject>
</root>

最后,这是我(非常糟糕)尝试XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/ /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="/">
        <xsl:copy>
            <xsl:apply-templates select="subject">
                <xsl:sort select="subjectCode"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="subject">
        <xsl:copy>
            <xsl:apply-templates select="course">
                <xsl:sort select="subjectAndNumber"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

你只有一些小错误 - 比较:

<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="/*">
    <xsl:copy>
        <xsl:apply-templates select="subject">
            <xsl:sort select="courseSubjectHeader/subjectCode"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="subject">
    <xsl:copy>
        <xsl:copy-of select="courseSubjectHeader"/>
        <xsl:apply-templates select="course">
            <xsl:sort select="subjectAndNumber"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

注意:您最严重的错误是:xmlns:xsl="http://www.w3.org/1999/ /Transform"。如果您没有正确声明XSLT命名空间,那么您的文档根本就不是样式表。