3

XSLT を使用して XML を XML に変換しようとしています。出力 XML は、入力 XML の ModificationTime 要素に基づいて並べ替える必要があります。以下はxmlコードです。

<?xml version="1.0" encoding="UTF-8"?>
<Process>
<currentDayAndHour>@Fri16</currentDayAndHour>

<!-- Few elements here. Need to retain them -->


<rowCount>1</rowCount>
<currentRow>1</currentRow>


<ClientList>
 <Status>0</Status>
 <ServerResponse>
  <Code>0</Code>
  <Text>OK</Text>
</ServerResponse>
<ServiceStartTime>2012-11-09 16:06:42.786</ServiceStartTime>
<ServiceEndTime>2012-11-09 16:06:42.827</ServiceEndTime>
<Files>
  <File>
    <Name>test.20121107215230411.txt</Name>
    <Size>29</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352343152</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>test.20121107183757513.txt</Name>
    <Size>29</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352331478</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>test1.20121107215230500.txt</Name>
    <Size>32</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352343152</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>test1.txt</Name>
    <Size>32</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352323788</ModificationTime>
    <Owner>65174</Owner>
    <Group>75431</Group>
  </File>
  <File>
    <Name>HMP_test.txt</Name>
    <Size>28</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352199478</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>test1.20121107183757585.txt</Name>
    <Size>32</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352331478</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>client_access.20121108101411179.txt</Name>
    <Size>4182</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352387653</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>TechMtngAgenda.txt</Name>
    <Size>107</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1352044842</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
  <File>
    <Name>test.txt</Name>
    <Size>29</Size>
    <Type>Regular</Type>
    <Permissions>-rw-r--r--</Permissions>
    <ModificationTime>1350063313</ModificationTime>
    <Owner>19737</Owner>
    <Group>70902</Group>
  </File>
</Files>
</ClientList>
<currentDocument>1</currentDocument>
</Process>

すべての入力要素を含む出力 XML が必要ですが、Files タグには ModificationTime の昇順で各ファイルを含める必要があります。私はXSLTが初めてです。xsl:sort を使用してみましたが、目的の結果を得ることができませんでした。

4

1 に答える 1

1

このスタイルシートは、XSLT 1.0 で必要なことを行います

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="Files">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="File">
        <xsl:sort order="ascending" data-type="number" select="ModificationTime"/>
      </xsl:apply-templates>      
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

このスタイルシートは、XSLT 2.0 で必要なことを行います

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

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

  <xsl:template match="Files">
    <xsl:copy>
      <xsl:sequence select="@*"/>
      <xsl:perform-sort select="File">
        <xsl:sort order="ascending" select="xs:integer(ModificationTime)"/>
      </xsl:perform-sort>      
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

どちらの場合も、Identity テンプレートを使用してドキュメントを正確にコピーし、シングルxsl:templateを導入してFiles要素の処理をオーバーライドします。XSLT 1.0 の例では、要素を でソートするためにxsl:apply-templatesが使用されています。ソートされた要素の処理はIdentityテンプレートに戻されるため、さらにオーバーライドされる可能性があります。xsl:sortFileModificationTime<xsl:apply-templates select="File">File

XSLT 2.0 の例xsl:sequenceでは、入力ノードを結果ツリーに直接挿入するために使用されます。同様xsl:perform-sortに、要素をコピーするための追加の命令を実行するのではなく、並べ替えられたシーケンスを直接返します。これらの変更により、スタイルシートの実行が高速化される可能性がありますが、将来のメンテナンスの柔軟性が低下することに注意してください。直接選択している場合、オーバーライドを追加するのははるかに困難です。XSLT 1.0 スタイルシートまたはその処理スタイルは、大きな変更なしで XSLT 2.0 で実行できます。最後に、これらの例は両方とも、要素ではない のnode()子を省略しています。XSLT 1.0 で追加できるものをキャプチャするにはFilesFile

<xsl:apply-templates select="node()[not(self::File)]"/>

または XSLT 2.0 のみ

<xsl:apply-templates select="node() except File"/>

また

<xsl:sequence select="node() except File"/>
于 2012-11-13T16:43:14.973 に答える