XPath 2.0:按属性检索节点,其中值不区分大小写

发布于 2024-12-04 12:44:26 字数 1214 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夢归不見 2024-12-11 12:44:26

您的示例 XML 和 XPath 语句不匹配。

  • 示例 XML 元素未绑定到命名空间。声明了“my”命名空间前缀,但未用于这些元素,因此它们位于“无命名空间”中。
  • 您的示例 XPath 在 Machine 的子元素上使用谓词过滤器,而不是在具有 @FQDNMachine 元素上使用。

您可以使用以下任一方法来查找不区分大小写的值:

  • matches() 函数,带有不区分大小写匹配的标志:

    //机器/机器[匹配(@FQDN,'machine2.mydomain.com','i')]
    
  • upper-case() 函数来计算大写字符串:

    //机器/机器[大写(@FQDN)=大写('machine2.mydomain.com')]
    
  • lower-case() 函数来计算小写字符串:

    //机器/机器[lower-case(@FQDN)=lower-case('machine2.mydomain.com')]
    

有人可以提供一个示例 XPath,其中包括处理
可以使用的命名空间吗?

不确定处理命名空间的含义,但如果您想匹配这些元素,而不管它们的命名空间如何,那么您可以对命名空间使用通配符运算符:

//*:Machines/*:Machine[matches(@FQDN,'machine2.mydomain.com','i')]

Your example XML and XPath statements don't match.

  • The sample XML elements are not bound to a namespace. The "my" namespace-prefix is declared, but not used for those elements, so they are in the "no namespace".
  • Your sample XPath is using predicate filters on the children of Machine rather than on the Machine 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:

    //Machines/Machine[matches(@FQDN,'machine2.mydomain.com','i')]
    
  • upper-case() function to evaluate the upper-case strings:

    //Machines/Machine[upper-case(@FQDN)=upper-case('machine2.mydomain.com')]
    
  • lower-case() function to evaluate the lower-case strings:

    //Machines/Machine[lower-case(@FQDN)=lower-case('machine2.mydomain.com')]
    

Can someone provide a sample XPath that includes handling of
Namespaces that would work?

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:

//*:Machines/*:Machine[matches(@FQDN,'machine2.mydomain.com','i')]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文