是否可以使用xsl对xml文件进行排序

时间:2010-02-02 09:02:23

标签: xml xslt

我有以下xml文件

<ScheduleProvider id="257" scheduleDate="2008-03-20T15:34:18Z" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="my.xsd">
    <Content action="insert" duration="7200" id="2" title="movie-2">
        <EpgDescription>
            <EpgElement key="Year">2002</EpgElement>
            <EpgElement key="Actors">Actor2 Actor22</EpgElement>
            <EpgElement key="Directors">Director2</EpgElement>
            <EpgElement key="Rating">2</EpgElement>
        </EpgDescription>
        <EpgDescription locale="en_US">
            <EpgElement key="Title">Blockbuster-2</EpgElement>
        </EpgDescription>
        <Media comment="" fileName="Asset_2" format="AV_ClearTS" frameDuration="180000" id="LYS008168695" title="Asset_2">
            <TechnicalMetadata key="ReadyForBroadcast">4</TechnicalMetadata>
            <TechnicalMetadata key="Subtitle_Languages"/>
        </Media>
    </Content>
    <Content action="insert" duration="7200" id="1" title="movie-1">
        <EpgDescription>
            <EpgElement key="Year">2001</EpgElement>
            <EpgElement key="Actors">Actor1 Actor11</EpgElement>
            <EpgElement key="Directors">Director1</EpgElement>
            <EpgElement key="Rating">1</EpgElement>
        </EpgDescription>
        <EpgDescription locale="en_US">
            <EpgElement key="Title">Blockbuster-1</EpgElement>
        </EpgDescription>
        <Media comment="" fileName="Asset_1" format="AV_ClearTS" frameDuration="180000" id="LYS008168695" title="Asset_1">
            <TechnicalMetadata key="ReadyForBroadcast">4</TechnicalMetadata>
            <TechnicalMetadata key="Subtitle_Languages"/>
        </Media>
    </Content>
</ScheduleProvider>

是否可以按 id 属性对其内容节点进行排序?我期待以下结果

<ScheduleProvider id="257" scheduleDate="2008-03-20T15:34:18Z" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="my.xsd">
    <Content action="insert" duration="7200" id="1" title="movie-1">
        <EpgDescription>
            <EpgElement key="Year">2001</EpgElement>
            <EpgElement key="Actors">Actor1 Actor11</EpgElement>
            <EpgElement key="Directors">Director1</EpgElement>
            <EpgElement key="Rating">1</EpgElement>
        </EpgDescription>
        <EpgDescription locale="en_US">
            <EpgElement key="Title">Blockbuster-1</EpgElement>
        </EpgDescription>
        <Media comment="" fileName="Asset_1" format="AV_ClearTS" frameDuration="180000" id="LYS008168695" title="Asset_1">
            <TechnicalMetadata key="ReadyForBroadcast">4</TechnicalMetadata>
            <TechnicalMetadata key="Subtitle_Languages"/>
        </Media>
    </Content>
    <Content action="insert" duration="7200" id="2" title="movie-2">
        <EpgDescription>
            <EpgElement key="Year">2002</EpgElement>
            <EpgElement key="Actors">Actor2 Actor22</EpgElement>
            <EpgElement key="Directors">Director2</EpgElement>
            <EpgElement key="Rating">2</EpgElement>
        </EpgDescription>
        <EpgDescription locale="en_US">
            <EpgElement key="Title">Blockbuster-2</EpgElement>
        </EpgDescription>
        <Media comment="" fileName="Asset_2" format="AV_ClearTS" frameDuration="180000" id="LYS008168695" title="Asset_2">
            <TechnicalMetadata key="ReadyForBroadcast">4</TechnicalMetadata>
            <TechnicalMetadata key="Subtitle_Languages"/>
        </Media>
    </Content>
</ScheduleProvider>

我听说过 xsl:sort ,我玩了一下。但我没有使用xslt的经验,我无法弄清楚如何使用 xsl:sort

2 个答案:

答案 0 :(得分:3)

你可以试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="ScheduleProvider">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="Content">
        <xsl:sort data-type="number" select="@id"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Content">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

您是否尝试过<xsl:sort select="id" />