2011-06-28 8 views
4

小さな例で助けが必要なので、xsl:sortをよく理解できます。XSLT:属性の並べ替え

私のXMLデータは次のようになります。

<NewTerms> 
    <newTerm ID="3">Zebra</newTerm> 
    <newTerm ID="11">Horse</newTerm> 
    <newTerm ID="1">Cat</newTerm> 
    <newTerm ID="90">Lion</newTerm> 
    <newTerm ID="62">Jaguar</newTerm> 
    <newTerm ID="30">Cheetah</newTerm> 
    <newTerm ID="55">Deer</newTerm> 
    <newTerm ID="45">Buffalo</newTerm> 
    <newTerm ID="15">Dog</newTerm> 
</NewTerms ID="10"> 

と私はID属性に応じてそれらをソートします。私が持っているXSLが動作していません。私はxsl:sort機能がどのように動作するか分からない

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

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> 

    <xsl:template match="@*|node()[not(preceding::node()=.)]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()[not(preceding::node()=.)]"> 
       <xsl:sort select="./@ID"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

。この例を理解して理解を深めるのを手伝ってください。

+0

[End-Tag](http://www.w3.org/TR/1998/REC-xml-19980210#NT-ETag)の属性を参照してください:D –

+0

+1基本的ですが使用完全な質問です。 –

答えて

1

NewTermsのClasingタグのID属性が不足しているようです。

次のXSLスクリプトはID属性にデータを並べ替え:@のRSPの回答にコメントで指摘したとおり

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

    <xsl:output method="xml"/> 

    <xsl:template match="@*|node()"> 
    <xsl:copy-of select="." /> 
    </xsl:template> 

    <xsl:template match="/NewTerms"> 
    <xsl:copy> 
     <xsl:apply-templates select="newTerm"> 
     <xsl:sort select="@ID" data-type="number" /> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+3

これらのID属性値は数字のように見えるので、 ' 'は、おそらく要求通りにソートする必要があります。デフォルトのデータタイプは「テキスト」で、「110」のように「12」より前になります。 –

+0

@マーティン、良い点。数値的にソートする答えを更新しました。 – rsp

+0

@Martin:これが主要なポイントです。 –

0

default sort data-typeは「テキスト」ですが、「数」をしたいです。あなたが実際にはその1つの変更を行った後、独自の最初の試みを使用することができます。これは、あなたの(非効率的)維持することができます

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

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> 

    <xsl:template match="@*|node()[not(preceding::node()=.)]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()[not(preceding::node()=.)]"> 
     <xsl:sort select="./@ID" data-type="number" /> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

チェックを複製:

node()[not(preceding::node()=.)] 
4

を変換あなたは、正しいです。 data-type属性を指定しないと、デフォルトでは「テキスト」なので、数字では機能しません。

xsl:sortの使用の詳細については、specsと最近の同様のquestionがあります。

ここではいくつかの注意事項:あなたが省略でき

  • テンプレートあなたはそのようにノードの前に確認する必要はありません
  • でデフォルトでコンテキストノードを選択./@ID.ので、それは意味がありません。

お知らせあなたはソート命令でのみIdentity Transformationを必要としています。

あなたの最後のようになります。変換:

<NewTerms> 
    <newTerm ID="3">Zebra</newTerm> 
    <newTerm ID="11">Horse</newTerm> 
    <newTerm ID="1">Cat</newTerm> 
    <newTerm ID="90">Lion</newTerm> 
    <newTerm ID="62">Jaguar</newTerm> 
    <newTerm ID="30">Cheetah</newTerm> 
    <newTerm ID="55">Deer</newTerm> 
    <newTerm ID="45">Buffalo</newTerm> 
    <newTerm ID="15">Dog</newTerm> 
</NewTerms> 

を生成します:

<NewTerms> 
    <newTerm ID="1">Cat</newTerm> 
    <newTerm ID="3">Zebra</newTerm> 
    <newTerm ID="11">Horse</newTerm> 
    <newTerm ID="15">Dog</newTerm> 
    <newTerm ID="30">Cheetah</newTerm> 
    <newTerm ID="45">Buffalo</newTerm> 
    <newTerm ID="55">Deer</newTerm> 
    <newTerm ID="62">Jaguar</newTerm> 
    <newTerm ID="90">Lion</newTerm> 
</NewTerms> 
(ここではあなたの終了タグのタイプミスを修正するために修正ビット)入力に適用されると

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

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"> 
       <xsl:sort select="@ID" data-type="number"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

+0

+1良い説明と簡単な解決のために – cordsen

+0

@cordsen、あなたのフィードバックをいただきありがとうございます。 –

関連する問題