當屬性缺失或爲空時,我想將我的變量設置爲false()
。如果某個屬性爲空或不存在,如何將XSLT布爾變量設置爲true?
XML:
<cd name="One" />
<cd name="Two" incollection=""/>
<cd name="Three" incollection="true"/>
XSL:
<!-- this will only set to false when attribute is missing -->
<xsl:variable name="incollection" select="boolean(@incollection)"/>
<!-- this will set to false both when attribute is missing or is empty-->
<xsl:variable name="incollection2" select="boolean(@incollection) or @incollection=''"/>
雖然第二次表達對我的作品,我想知道是否有寫這表達更好,更地道的方式?
@empo您正在使用哪種XSLT處理器?不適合我,例如,內置在Chrome或Java Xalan中的XSLT處理器 –
好問題,+1。查看我的答案,瞭解兩種可選的單行XPath表達式。 –