我有幾個節點具有相同的Name ='UPC',我需要找到當前節點的值。xpath找到當前節點的值
<XML>
<Attribute>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>Testing</Value>
</Attribute>
<Attribute>
<Name>UPC</Name>
<Type>ComplexAttr</Type>
<Value>24a</Value>
</Attribute>
</XML>
預期輸出: 應該從拉/屬性/值,其中名稱= 'UPC' 和類型= 'ComplexAttr' 的值。
在第一次運行=「測試」 & 在第二次運行值應爲=「24A」
我嘗試使用下面的代碼,但它無法正常工作。該值爲空。
<xsl:attribute name ="value">
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][$i]/Value" />
</xsl:attribute>
其中$ I是我通過上面的XML使用循環的變量,它每次運行後遞增。但是,它在每次運行中只給出相同的值'測試'(這是第一個值)。我檢查了變量的值。它每次循環時都在變化。
我也嘗試過使用current()和position(),但是在這種情況下我得到了null。
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][current()]/Value" />
<xsl:value-of select =".//Attribute[Type='ComplexAttr' and Name = 'UPC'][position() = $i]/Value" />
有人可以幫我解決這個問題。提前致謝。
此XML樣本的預期輸出是什麼? – 2011-04-12 23:17:32
@Alejandro。我已經添加了上面的預期輸出。謝謝 – JohnXsl 2011-04-13 00:35:24
好問題,+1。請參閱我的答案以獲得解釋和快速的單線XPath表達式解決方案。 – 2011-04-13 03:18:01