2011-10-30 31 views
0

我有一個這樣的XML:XSLT 2.0 - 遍歷節點集的變量,但需要處理在循環的其他元素以及

<?xml version="1.0" encoding="UTF-8"?> 

<nodes> 
    <n c="value2"/> 
    <n>Has a relation to node with value2</n> 
    <n>Has a relation to node with value2</n> 
    <n c="value"/> 
    <n>Has a relation to node with value</n> 
    <n c="value1"/> 
    <n>Has a relation to node with value1</n> 
</nodes> 

我排序所有有屬性在可變元素,那麼我迭代在的這個變量中,對於每個循環。但是在每個循環結束時,我需要打印當前處理元素下面的元素的值(原始XML),並且沒有混亂。

這意味着:在沒有屬性的<n>上調用apply-templates,但是「select」attr。在apply-templates不起作用,可能是因爲我現在在變量循環

有沒有解決方案? 感謝

這裏是XSL:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:template match="/"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="nodes"> 

     <xsl:variable name="sorted"> 
      <xsl:for-each select="n[@c]"> 
       <xsl:sort select="@c"></xsl:sort> 
       <xsl:copy-of select="."></xsl:copy-of> 
      </xsl:for-each> 
     </xsl:variable> 

     <xsl:for-each select="$sorted/n"> 
      <xsl:value-of select="@c"></xsl:value-of> 

      <xsl:apply-templates select="/nodes/n[2]"></xsl:apply-templates> 
     </xsl:for-each> 

    </xsl:template> 

    <xsl:template match="n[not(@c)]"> 
     <xsl:value-of select="."></xsl:value-of> 
    </xsl:template> 

</xsl:stylesheet> 

這僅僅是例子,這一切都是大項目:)

所需的輸出具有更復雜的XPath(現在連一個簡單的一部分不起作用):

Value 
Has a relation to node with value 
Value1 
Has a relation to node with value1 
Value2 
Has a relation to node with value2 
Has a relation to node with value2 

現在有點清楚了嗎?

回答

0

一些想法:apply-templates without select處理當前上下文節點的子節點;在您的輸入示例中,n元素根本沒有任何子元素。此外,在你的變量中,你做一個拷貝 - 意味着你創建了與輸入樣本中的節點無關的新節點。因此,雖然我不確定您希望通過for-templates中的apply-templates實現您的構建,但每個都沒有意義,因爲您已發佈的輸入示例以及您使用的變量。

我懷疑你可以使用XSLT 2.0,每個組羣的出發與在

<xsl:template match="nodes"> 
    <xsl:for-each-group select="n" group-starting-with="n[@c]"> 
    <xsl:sort select="@c"/> 
    <xsl:value-of select="@c"/> 
    <xsl:apply-templates select="current-group() except ."/> 
    </xsl:for-each-group> 
</xsl:template> 

如果沒有幫助,然後考慮後用樣本數據的很小的輸入採樣和相應的輸出你想用XSLT 2.0創建的樣本,那麼我們可以就如何實現這一點提出建議。

[編輯]現在你已經發布了一個輸出採樣我後我以前建議的增強版本:

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

<xsl:output method="text"/> 

<xsl:template match="nodes"> 
    <xsl:for-each-group select="n" group-starting-with="n[@c]"> 
    <xsl:sort select="@c"/> 
    <xsl:value-of select="@c"/> 
    <xsl:text>&#10;</xsl:text> 
    <xsl:apply-templates select="current-group() except ."/> 
    </xsl:for-each-group> 
</xsl:template> 

<xsl:template match="n[not(@c)]"> 
    <xsl:value-of select="."/> 
    <xsl:text>&#10;</xsl:text> 
</xsl:template> 

</xsl:stylesheet> 

當我使用撒克遜9.3和運行對你的最新的輸入樣本樣式表中的結果是如下:

value 
Has a relation to node with value 
value1 
Has a relation to node with value1 
value2 
Has a relation to node with value2 
Has a relation to node with value2 

這就是你所要求的我認爲所以試試這種方法與更復雜的實際輸入。

+0

謝謝你,我會改進我原來的例子,使它更有意義。 – MartinM

+0

完美的謝謝,我只是試圖用更復雜的方式。對於每個組是一個很好的解決方案... – MartinM