How to set an XSLT boolean variable to true if an attribute is empty or doesn't exist?
When an attribute is missing or is empty, I want my variable set to false()
.
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:variabl开发者_如何学运维e name="incollection2" select="boolean(@incollection) or @incollection=''"/>
Although the second expression works for me, I was wondering whether there was a better and more idiomatic way of writing this expression?
Use:
string-length(@incollection) > 0
In case you want to exclude an attribute whose string value is white-space only, use:
string-length(normalize-space(@incollection)) > 0
Personally I would be inclined to try something like
select="count(@incollection != '') > 0
(untested)
精彩评论