2010-10-25 52 views
1
XML元素

假設我有一個像這樣訂購使用XSLT

XML文件的XML文檔:

<document> 
    <educationalsection> 
    educational details 
    </educationalsection> 

    <professionalsection> 
    professional section details 
    </professionalsection> 
</document> 

我創建了XSL將其融合到我需要的格式,但問題是,如果我想改變部分的順序我該怎麼做?例如,如果我想專業部分來教育的頂部而不改變XML,那怎麼可能?我需要添加到現有的xsl或xml中,以便當我的Web應用程序發送xml進行轉換時,它可以具有Web應用程序指定的不同元素順序。

回答

2

該樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <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> 
</xsl:stylesheet> 

輸出:

<document> 
    <professionalsection> 
    professional section details 
    </professionalsection> 
    <educationalsection> 
    educational details 
    </educationalsection> 
</document> 
+0

這是一個動態的解決方案?這意味着你可以自動設置部分的順序? – 2010-10-25 12:45:20

+0

@Sultan Saadat:您應該將新訂單字符串作爲'pOrder'參數傳遞給處理器。 – 2010-10-25 13:25:56

+0

謝謝你的幫助:)這真的很有幫助! :) – 2010-10-26 08:51:32

1

xsl:apply-templatesxsl:for-each元素可以具有xsl:sort子元素,可用於排序所選子節點。在需要時使用不同的排序順序。

您還可以使用xsl:template上的mode屬性使用不同的排序順序選擇不同的模板。

+0

爲模式的任何代碼示例? – 2010-10-25 11:42:05

+0

@Sultan Saadat - 'mode'的例子。這些不是特定於排序,但應該給你的總體思路。 http://zvon.org/xxl/XSLTreference/OutputExamples/example_1_20_frame.html – Oded 2010-10-25 11:47:26

+0

我明白了,但我的部分是不同的,所以我將不得不使用部分序列號字段的類型優先每個部分,然後反映在輸出... – 2010-10-25 11:51:03