使用 XOM 具有不区分大小写内容的节点

发布于 2024-10-15 01:49:42 字数 394 浏览 0 评论 0原文

我想从包含特定值但不区分大小写的 XOM 文档中查询节点。像这样的东西:

doc.query('/root/book[contains(.,"case-insentive-string")]')

但它包含区分大小写。

  1. 我尝试使用正则表达式,但它是 只有XPATH2.0,而XOM好像没有 支持它。
  2. 我试过 contains(translate(."ABCEDF...","abcdef..."),"case-insentive-string")]' 也失败了。
  3. 我尝试匹配 子节点并读取父节点属性 使用 getParent,但没有 读取父母属性的方法。

有什么建议吗?

I want to query nodes from a XOM document which contains certain value but case insensitive. Something like this:

doc.query('/root/book[contains(.,"case-insentive-string")]')

But it contains is case sensitive.

  1. I tried to use regexes, but it is
    only XPATH2.0 and XOM does not seem
    to support it.
  2. I tried
    contains(translate(."ABCEDF...","abcdef..."),"case-insentive-string")]'
    failed too.
  3. I tried to match
    subnodes and read parent attributes
    using getParent, but there is no
    method to read parents attributes.

Any suggestions ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

゛时过境迁 2024-10-22 01:49:42

如果您使用的是 XOM,那么您可以使用 Saxon 对其运行 XPath 或 XQuery。这使您能够使用 XPath 2.0 中大大增加的函数库,其中包括函数 lower-case() 和 upper-case(),并且还能够(尽管以某种特定于产品的方式)选择您自己的排序规则与 contains() 等函数一起使用 - 这意味着您可以进行忽略重音和大小写的匹配。

If you are using XOM, then you can use Saxon to run XPath or XQuery against it. That gives you the ability to use the greatly increased function library in XPath 2.0, which includes functions lower-case() and upper-case(), and also the ability (though in a somewhat product-specific way) to choose your own collations for use with functions such as contains() - which means you can do matching that ignores accents as well as case, for example.

樱桃奶球 2024-10-22 01:49:42

2.我尝试了 contains(translate(."ABCEDF...","abcdef..."),"case-insentive-string")]'
也失败了。

正确的编写方法是

/root/book[contains(translate(., $vUpper, $vLower),
                    translate($vCaseInsentiveString, $vUpper, $vLower)
                    )
          ]

其中 $vUpper$vLower 被定义为(应替换为)字符串:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

and

'abcdefghijklmnopqrstuvwxyz'

和 < code>$vCaseInsentiveString 定义为(应替换为)特定的不区分大小写的字符串。

例如,给定以下 XML 文档

<authors>
  <author>
    <name>Victor Hugo & Co.</name>
    <nationality>French</nationality>
  </author>
  <author period="classical" category="children">
    <name>J.K.Rollings</name>
    <nationality>British</nationality>
  </author>
  <author period="classical">
    <name>Sophocles</name>
    <nationality>Greek</nationality>
  </author>
  <author>
    <name>Leo Tolstoy</name>
    <nationality>Russian</nationality>
  </author>
  <author>
    <name>Alexander Pushkin</name>
    <nationality>Russian</nationality>
  </author>
  <author period="classical">
    <name>Plato</name>
    <nationality>Greek</nationality>
  </author>
</authors>

以下 XPath 表达式(用相应的字符串替换变量):

   /*/author/name
              [contains(translate(., $vUpper, $vLower),
                        translate('lEo', $vUpper, $vLower)
                        )
              ]

选择此元素

<name>Leo Tolstoy</name>

说明contains()函数的两个参数都转换为小写,然后进行比较。

2.I tried contains(translate(."ABCEDF...","abcdef..."),"case-insentive-string")]'
failed too.

The proper way to write this is:

/root/book[contains(translate(., $vUpper, $vLower),
                    translate($vCaseInsentiveString, $vUpper, $vLower)
                    )
          ]

where $vUpper and $vLower are defined as (should be substituted by) the strings:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

and

'abcdefghijklmnopqrstuvwxyz'

and $vCaseInsentiveString is defined as (should be substituted by) the specific case-insensitive string.

For example, given the following XML document:

<authors>
  <author>
    <name>Victor Hugo & Co.</name>
    <nationality>French</nationality>
  </author>
  <author period="classical" category="children">
    <name>J.K.Rollings</name>
    <nationality>British</nationality>
  </author>
  <author period="classical">
    <name>Sophocles</name>
    <nationality>Greek</nationality>
  </author>
  <author>
    <name>Leo Tolstoy</name>
    <nationality>Russian</nationality>
  </author>
  <author>
    <name>Alexander Pushkin</name>
    <nationality>Russian</nationality>
  </author>
  <author period="classical">
    <name>Plato</name>
    <nationality>Greek</nationality>
  </author>
</authors>

the following XPath expression (substitute the variables by the corresponding strings):

   /*/author/name
              [contains(translate(., $vUpper, $vLower),
                        translate('lEo', $vUpper, $vLower)
                        )
              ]

selects this element:

<name>Leo Tolstoy</name>

Explanation: Both arguments of the contains() function are converted to lower-case, and then the comparison is performed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文