【问题标题】:XPath matching wildcards?XPath 匹配通配符?
【发布时间】:2017-11-22 07:31:41
【问题描述】:

希望在属性为路径的 xpath 中使用通配符,我想要所有以“文本”结尾的路径。

示例<Property name="jcr:content/MainParsys/*/text"/>

  • jcr:content/MainParsys 是常量。
  • * 是我想要通配符的位置(中间的所有路径)。
  • text 是我正在寻找的路径的最后一部分。

此外,我想避免以/textIsRich 结尾的路径。

寻找: <Property name="jcr:content/MainParsys/*/text"/>

避免: <Property name="jcr:content/MainParsys/*/text *"/>

【问题讨论】:

    标签: xml xpath


    【解决方案1】:

    XPath 1.0

    这个 XPath,

    //Property[@name[  starts-with(.,'jcr:content/MainParsys/')
             and substring(., string-length(.) - string-length('/text') +1) = '/text']]
    

    将选择所有具有name 属性的Property 元素,其值以'jcr:content/MainParsys/' 开头并以'/text' 结尾(使用标准XPath 1.0 work-around for no ends-with() function)。

    XPath 2.0

    XPath 2.0 具有 ends-with() 以及诸如 matches() 之类的正则表达式函数,可以匹配有效的通配符 (.*)。

    【讨论】:

    • @matt:哎呀,你当然是对的。答案更正。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多