如何在 XPath 中选择布尔值节点? [英] How to select boolean-valued nodes in XPath?

查看:23
本文介绍了如何在 XPath 中选择布尔值节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文档和关联的架构,该架构将多个属性定义为具有 xs:boolean 类型.xs:boolean 的词法值为 truefalse10>,所以似乎要正确选择具有特定布尔值的属性,我必须编写如下内容:

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'

@attribute='false' or @attribute='0'

这看起来很冗长.

您可能希望 boolean(@attribute) 之类的东西可以工作,但是 boolean 函数有不同的语义.

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?

推荐答案

除了 Phil 和 Tomalak 提出的解决方案,我发现 XPath 2.0 提供了一些替代方案:

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

最后,XPath 2.0 确实提供了模式感知处理,这意味着如果一切都对齐,您应该能够编写:

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)

但是模式感知处理器似乎很难获得.最受欢迎的似乎是 saxon 的非免费商业变体,售价 300 英镑.所以现在我使用 @attribute=('true','1').

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').

这篇关于如何在 XPath 中选择布尔值节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆