开发者

is it possible to sort an xml file using xsl

I have the following xml file

<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>

Is it possible to sort its Content nodes by the attribute id? I'm expecting the following result

<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>
   开发者_如何学Go     </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>

I've heard about xsl:sort and I played with it a bit. But I have no experience with xslt and I cannot figure out how to use the xsl:sort .


You could try this:

<?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>


Have you tried <xsl:sort select="id" />?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜