2010-08-09 64 views
0

我有一個XML文件,其內容我想按文檔順序排序(基本上是按照項目寫出的順序)。XSL按位置排序()給出奇怪的結果

我目前使用以下代碼:

<xsl:template match="/Error"> 
     <xsl:apply-templates> 
      <xsl:sort select="position()" order="descending" /> 
     </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="/Error/Warning"> 
<!-- etc --> 
</xsl:template> 

實施例XML(數據替換爲了便於閱讀):

<Error> 
<Warning data="stuff" timestamp="08:26:17 2010/08/01">CODE.1</Warning> 
<Clear data="stuff" timestamp="08:26:36 2010/08/01">CODE.2</Clear> 
<Warning data="stuff" timestamp="08:36:00 2010/08/01">CODE.3</Warning> 
<Clear data="stuff" timestamp="08:36:56 2010/08/01">CODE.4</Clear> 
<Warning data="stuff" timestamp="08:40:31 2010/08/01">CODE.5</Warning> 
</Error> 

然而,這似乎給奇怪的結果,因爲它似乎是沒有特別的順序!有任何想法嗎?

刪除排序似乎使它正常工作 - 這是否可靠地命令它在寫入順序或不能保證?

+0

您不提供有問題的輸出,也不能解釋您未顯示的輸出有什麼問題。此外,您不指定真正需要排序的內容。這個問題是不完整的和未定義的。請糾正你的問題。 – 2010-08-09 11:53:06

+0

它看起來像你的模式是錯誤的,它應該是'ErrorLog/Warning'。你也是按文件順序排序,所以沒有必要這樣做。 – 2010-08-09 12:44:14

+0

@Dimitre輸出結果是隨機排列的,我不覺得這很重要嗎? - 更新的問題,使其更清晰 @Alejandro我想要命令''內的所有東西。下面的Paul Butchers迴應對我所需要的很好。 – Chris 2010-08-09 14:18:14

回答

1

<xsl:apply-templates />以文檔順序對選定的節點集進行操作,刪除排序元素,這將按需要工作。參見:Applying Template Rules

1

您是否錯過了想要將模板應用到哪個節點?

例如:

<xsl:apply-templates select="/Error/messages" /> 

將是不錯的您正在使用XSLT處理問題時使用的XML。

+0

更新了一些信息... – Chris 2010-08-09 09:50:03

1

難道不是這樣嗎?使用apply-templates上的select屬性?

<xsl:template match="/Error"> 
    <xsl:apply-templates select="./Warning" /> 
</xsl:template> 

<xsl:template match="/Error/Warning"> 
    <!-- etc --> 
</xsl:template> 

您應該按照它在XML源代碼中的順序獲取輸出。

+0

@Chris如果你想過濾錯誤的孩子,並且只有警告元素,最好使用'xsl:apply-templates/@ select',就像在這個答案中定義的那樣,而不僅僅是使用'xsl:template/@ match' – 2010-08-09 16:54:37