2013-03-24 138 views
1

如何使用xslt.can中的每個循環比較以前的Item1值與當前的item1值,您可以告知我。下面是輸入。如何比較以前的節點元素值與foreach中的當前節點元素值xslt

輸入:

<t> 
<Items> 
<Item1>24</Item1> 

</Items> 

<Items> 
<Item1>25</Item1> 

</Items> 

<Items> 
<Item1>25</Item1> 

</Items> 

</t> 

輸出:

<t> 

<xsl:for-each select="Items"> 

<xsl:if previos Item1 != current Item1><!-- compare previous item1 with current Item1 --> 





</xsl:for-each> 
</t> 
+0

您尚未提供轉換所需的結果 - 您是否嘗試去除Item1元素? – 2013-03-24 15:27:14

回答

1

可以使用preceding-siblingaxis,例如像這樣:

not(preceding-sibling::Items[1]/Item1 = Item1) 
+0

除了節點列表中的項目通常不是兄弟姐妹。 – 2013-03-24 15:25:32

1

不要試圖在思考這個術語「迭代」,而是考慮如何爲選擇正確的節點在第一位。它看起來像你想只處理項目元素,其Item1是不一樣的輸入樹直接前置兄弟

<xsl:for-each select="Items[preceding-sibling::Items[1]/Item1 != Item1]"> 

如果你想用XSLT很大進展,你需要停下來思考類似程序的事情循環和分配,而是學習在功能上思考 - 我想要的輸出與我從哪裏開始的輸入有關。

+0

除了節點列表中的項目通常不是兄弟姐妹。 – 2013-03-24 15:25:48

2

這裏是一般情況下的通用解決方案時,在節點列表中的項目都沒有兄弟姐妹(甚至可能屬於不同的文件):

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

<xsl:template match="/*"> 
    <xsl:apply-templates select="Items/Item1"> 
     <xsl:with-param name="pNodeList" select="Items/Item1"/> 
    </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="Item1"> 
    <xsl:param name="pNodeList"/> 

    <xsl:variable name="vPos" select="position()"/> 
    <xsl:copy-of select="self::node()[not(. = $pNodeList[$vPos -1])]"/> 
</xsl:template> 
</xsl:stylesheet> 

當應用這種轉變所提供的XML文檔:

<t> 
    <Items> 
     <Item1>24</Item1> 
    </Items> 
    <Items> 
     <Item1>25</Item1> 
    </Items> 
    <Items> 
     <Item1>25</Item1> 
    </Items> 
</t> 

有用,(假定)正確的結果產生:

<Item1>24</Item1> 
<Item1>25</Item1> 
相關問題