2016-07-21 13 views
0

私はXSLTの新機能です。私はウェブでかなりの量の研究をしましたが、私のケースに合った十分な情報を見つけることができませんでした。XSLTのソート入力は任意の順序で

これは私が達成しようとしているものです: 私は10から15セクションの大きなXML文書を持っています。以下のような構造多かれ少なかれルックスは:

<headerSection> 
    <!-- lots of stuff in here --> 
</headerSection> 
<containerSection> 
    <bodySection> 
     <container1> 
      <container2> 
       <identifier code="12345"/> 
       <!-- lots of stuff in here --> 
      </container2> 
     </container1> 
     <container1> 
      <container2> 
       <identifier code="98765"/> 
       <!-- lots of stuff in here --> 
      </container2> 
     </container1> 
    </bodySection> 
</containerSection> 

私は私が出力するために、このようにスタイル設定HTMLページを適用するXSLファイルを持っています。 私がしたいことは、<bodySection>の子を任意の順序でレンダリングすることです。順序を決定する基準は、<identifier>要素のcode属性です。
注文を恣意的にすることをもう一度明らかにする。

私はxsl:sortディレクティブでこれを行うことができますが、私は巨大なXSL初心者です。私はオンラインで見つかった例からそれを使用する方法はあまりありません。
主なポイントは、XML入力を「ちょうど」変換するのではなく、スタイル付きのHTMLページを生成することです。私がやろうとしているものに最も近いことをどのように見えるか

はこのスニペットです:

<xsl:param name="pOrder" select="'professionalsection,educationalsection'"/> 
<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"> 
      <xsl:sort select="string-length(
           substring-before(
            concat(',',$pOrder,','), 
            concat(',',name(),',')))"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

残念ながら、私は私の場合には、それを使用する方法については非常によく分かりません。 直感的に、私はselect="'professionalsection,educationalsection'"で指し示されている値を変更しようとしますが、これは正しいアプローチですか? ご迷惑をおかけいたします。

UPDATE

コメントに投稿された例を見ることによって、私はこれを思い付きました。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="bodySection"> 
    <xsl:variable name="sort-order">|98765|12345|</xsl:variable> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"> 
      <xsl:sort select="string-length(substring-before($sort-order, concat('|', container1/container2/identifier/@code, '|')))" data-type="number" order="descending"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

上記のスニペットはまだ動作していません。 (子供たちと一緒に)要素がcontainer1交換され

<headerSection> 
    <!-- lots of stuff in here --> 
</headerSection> 
<containerSection> 
    <bodySection> 
     <container1> 
      <container2> 
       <identifier code="98765"/> 
       <!-- lots of stuff in here --> 
      </container2> 
     </container1> 
     <container1> 
      <container2> 
       <identifier code="12345"/> 
       <!-- lots of stuff in here --> 
      </container2> 
     </container1> 
    </bodySection> 
</containerSection> 

...:私は達成しようとしている結果は次のようになります。

+0

を参照してください。http://stackoverflow.com/questions/38409952/xslt-order-classes-in-a-non-alphabetical-non-numerical-order/38410077 #38410077そうでなければ、必要なロジックを理解できるように質問を展開してください:あなたは 'code'値でソートしたいと言っていますが、あなたの例では' professionalsection'や 'educationalsection'という値のコードはありません。 –

+0

こんにちは返信いただきありがとうございます。あなたが 'profectionection'に' code'属性を表示していないのは、これが最も有望なオンラインで見つかったスニペットだからです。 私はそれを私のケースに適応しようとしています: 私がこれまでに出てきたことは次のとおりです: SolarDev

+0

これはあなたの質問が何であるかを理解するのに役立ちません。とにかく、あなたの答えを見つけたことを願っています。 –

答えて

0

試してみてください。この場合に役立ちます

<xsl:sort select="string-length(substring-before($sort-order, concat('|', container2/identifier/@code, '|')))" data-type="number" order="ascending"/> 
+0

いいえ、動作しません。出力は入力 – SolarDev

+0

と同じです。*動作しません。 http://xsltransform.net/94rmq7k –

+0

それは本当に動作します!私は正しい名前空間接頭辞を宣言するのを忘れていたので、最初はありませんでした。マイケルありがとう – SolarDev

関連する問題