2011-05-16 26 views
1

我有這樣的XML如何識別使用xsl我的xml中是否存在節點?

<game> 
<genre> 
    <action>...</action> 
    <racing>...</racing> 
<price> 
.. 
.. 
</price> 
</genre> 
</game> 

我想檢查價格節點是否存在使用XSL的XML。我怎樣才能做到這一點?如果價格節點存在,則調用特定template其他調用另一個template

回答

2

你可以簡單地調用xsl:apply-templates與匹配屬性設置爲元素名稱。如果該元素存在,則調用該模板。它不存在,模板將不會被調用。 如果你正在試圖建立一個if-else語句,你可以檢查是否存在這樣的

<xsl:choose> 
    <xsl:when test="boolean(price)"> 
     <!-- do something --> 
    </xsl:when> 
    <xsl:otherwise> 
     <!-- do something else --> 
    </xsl:otherwise> 
</xsl:choose> 

所以,你可以檢查元素的存在,並作出相應的反應。

+0

謝謝,它解決了我的問題 – abi1964 2011-05-16 09:19:39

+0

關於此問題的一些文檔:http://www.w3.org/TR/xpath-functions/#func-boolean http://msdn.microsoft.com/en-us/library /ms256159.aspx – 2013-01-28 17:08:09

相關問題