I have an XML document and associated schema that defines several attributes as having the xs:boolean
type. The lexical values for xs:boolean
are true
, false
, 1
, and 0
, so it seems that to correctly select attributes with a particular boolean value I'd have to write something like:
@attribute='true' or @attribute='1'
or
@attribute='false' or @attribute='0'
This seems verbose.
You might expect something like boolean(@attribute)
to work, but the boolean
function has different semantics.
Is there a better way? Is this something that a schema-aware processor would help with?
In addition to the solutions proposed by Phil and Tomalak, I discovered that XPath 2.0 provides a few alternatives:
@attribute=('true','1')
string(@attribute) cast as xs:boolean
And finally, XPath 2.0 does provide schema-aware processing, which means that if everything is in alignment, you should be able to write:
data(@attribute)
But schema-aware processors seem hard to come by. The most popular seems to be the non-free commercial variant of saxon, which costs £300. So for now I'm using @attribute=('true','1')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With