XPath 2.0:按属性检索节点,其中值不区分大小写
我是使用 XPath 的新手,我试图通过节点的属性检索节点,但问题是该属性不区分大小写,这意味着我无法确切知道字符串在文档中的大小写。
例如:
给定文档:
<Document xmlns:my="http://www.MyDomain.com/MySchemaInstance">
<Machines>
<Machine FQDN="machine1.mydomain.com">
<...>
</Machine>
<Machine FQDN="Machine2.MyDomain.Com">
<...>
</Machine>
</Machines>
</Document>
如果我想检索 machine1,我会使用 XPath:
//my:Machines/my:Machine/*[@FQDN='machine1.mydomain.com']
但是获取 machine2 的类似 XPath 将失败,因为大小写不匹配:
//my:Machines/my:Machine/*[@FQDN='machine2.mydomain.com'] //Fails
我已经看到各种帖子提到使用类似的内容(我不是确定如何将命名空间应用于此):
translate(@FQDN, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
但即使我让它工作,考虑到我使用它的次数,它也会非常麻烦。
最后,我读到 XPath 2.0 支持 matches() 和 lower-case(),但作为 XPath 的新手,我不明白如何应用它们:
例如,如果我尝试以下操作,我会得到“无效的合格名称”: //my:Machines/my:Machine/[matches(@FQDN, '(?i)machine1.mydomain.com')] //my:Machines/my:Machine/[lower-case(@FQDN, 'machine1.mydomain.com')]
有人可以提供一个包含有效命名空间处理的示例 XPath 吗?
谢谢
I am new to using XPath and I am trying to retrieve a node via its attribute but the problem is that the attribute is case insensitive meaning I won't exactly know how the string is cased in the document.
So for example:
Given the document:
<Document xmlns:my="http://www.MyDomain.com/MySchemaInstance">
<Machines>
<Machine FQDN="machine1.mydomain.com">
<...>
</Machine>
<Machine FQDN="Machine2.MyDomain.Com">
<...>
</Machine>
</Machines>
</Document>
If I want to retrieve the machine1 I would use the XPath:
//my:Machines/my:Machine/*[@FQDN='machine1.mydomain.com']
But a similar XPath to get machine2 would fail becuase the case does not match:
//my:Machines/my:Machine/*[@FQDN='machine2.mydomain.com'] //Fails
I have seen various posts mention using something like (I am not sure how to apply Namespaces to this):
translate(@FQDN, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
But even if I got it to work it would be really cumbersome considering the number of times I would be using it.
Finally I have read that XPath 2.0 supports matches() and lower-case() but being new to XPath I don't understand how to apply them:
For example if I try the following I get an "Invalid Qualified name":
//my:Machines/my:Machine/[matches(@FQDN, '(?i)machine1.mydomain.com')]
//my:Machines/my:Machine/[lower-case(@FQDN, 'machine1.mydomain.com')]
Can someone provide a sample XPath that includes handling of Namespaces that would work?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例 XML 和 XPath 语句不匹配。
Machine
的子元素上使用谓词过滤器,而不是在具有@FQDN
的Machine
元素上使用。您可以使用以下任一方法来查找不区分大小写的值:
matches()
函数,带有不区分大小写匹配的标志:upper-case()
函数来计算大写字符串:lower-case()
函数来计算小写字符串:不确定处理命名空间的含义,但如果您想匹配这些元素,而不管它们的命名空间如何,那么您可以对命名空间使用通配符运算符:
Your example XML and XPath statements don't match.
Machine
rather than on theMachine
element that has the@FQDN
.You could use either of these methods to look for the value case-insensitive:
matches()
function, with a flag for case-insensitive matching:upper-case()
function to evaluate the upper-case strings:lower-case()
function to evaluate the lower-case strings:Not sure what you meant by the handling of namespaces, but if you wanted to match on those elements regardless of their namespace then you can use the wildcard operator for the namespace: