【问题标题】:How can I write this XPath 2.0 query in XPath 1.0如何在 XPath 1.0 中编写此 XPath 2.0 查询
【发布时间】:2014-10-01 12:08:32
【问题描述】:

我有一个 XPath 2.0 查询(看起来没问题,它返回我希望它返回的节点),但是我需要用 XPath 1.0 编写它。有可能吗?如果可以,我该怎么做?

信息:

我想要做的是将IDENTIFIERqualifiedIdentifier 下面的所有tokenValue 节点的tokenValue 连接起来,如果这种连接导致一个以“another.pkg”开头的字符串,则选择qualifiedIdentifier 节点”。

XPath 2.0 查询:

//importDeclaration/qualifiedIdentifier[starts-with(string-join(descendant::*/*/@tokenValue,''),'another.pkg')]

xml:

<compilationUnit tokenValue="package" tokenLine="1" tokenColumn="0">
  <packageDeclaration tokenValue="package" tokenLine="1" tokenColumn="0">
    <PACKAGE tokenValue="package" tokenLine="1" tokenColumn="0">
      <TOKEN tokenValue="package" tokenLine="1" tokenColumn="0"/>
    </PACKAGE>
    <qualifiedIdentifier tokenValue="my" tokenLine="1" tokenColumn="8">
      <IDENTIFIER tokenValue="my" tokenLine="1" tokenColumn="8">
        <TOKEN tokenValue="my" tokenLine="1" tokenColumn="8"/>
      </IDENTIFIER>
      <DOT tokenValue="." tokenLine="1" tokenColumn="10">
        <TOKEN tokenValue="." tokenLine="1" tokenColumn="10"/>
      </DOT>
      <IDENTIFIER tokenValue="pkg" tokenLine="1" tokenColumn="11">
        <TOKEN tokenValue="pkg" tokenLine="1" tokenColumn="11"/>
      </IDENTIFIER>
    </qualifiedIdentifier>
    <SEMI tokenValue=";" tokenLine="1" tokenColumn="14">
      <TOKEN tokenValue=";" tokenLine="1" tokenColumn="14"/>
    </SEMI>
  </packageDeclaration>
  <importDeclaration tokenValue="import" tokenLine="3" tokenColumn="0">
    <IMPORT tokenValue="import" tokenLine="3" tokenColumn="0">
      <TOKEN tokenValue="import" tokenLine="3" tokenColumn="0"/>
    </IMPORT>
    <qualifiedIdentifier tokenValue="another" tokenLine="3" tokenColumn="7">
      <IDENTIFIER tokenValue="another" tokenLine="3" tokenColumn="7">
        <TOKEN tokenValue="another" tokenLine="3" tokenColumn="7"/>
      </IDENTIFIER>
      <DOT tokenValue="." tokenLine="3" tokenColumn="14">
        <TOKEN tokenValue="." tokenLine="3" tokenColumn="14"/>
      </DOT>
      <IDENTIFIER tokenValue="pkg" tokenLine="3" tokenColumn="15">
        <TOKEN tokenValue="pkg" tokenLine="3" tokenColumn="15"/>
      </IDENTIFIER>
      <DOT tokenValue="." tokenLine="3" tokenColumn="18">
        <TOKEN tokenValue="." tokenLine="3" tokenColumn="18"/>
      </DOT>
      <IDENTIFIER tokenValue="AnotherClass" tokenLine="3" tokenColumn="19">
        <TOKEN tokenValue="AnotherClass" tokenLine="3" tokenColumn="19"/>
      </IDENTIFIER>
    </qualifiedIdentifier>
    <SEMI tokenValue=";" tokenLine="3" tokenColumn="31">
      <TOKEN tokenValue=";" tokenLine="3" tokenColumn="31"/>
    </SEMI>
  </importDeclaration>
  <typeDeclaration tokenValue="public" tokenLine="5" tokenColumn="0">
    <modifier tokenValue="public" tokenLine="5" tokenColumn="0">
      <PUBLIC tokenValue="public" tokenLine="5" tokenColumn="0">
        <TOKEN tokenValue="public" tokenLine="5" tokenColumn="0"/>
      </PUBLIC>
    </modifier>
    <classDeclaration tokenValue="class" tokenLine="5" tokenColumn="7">
      <CLASS tokenValue="class" tokenLine="5" tokenColumn="7">
        <TOKEN tokenValue="class" tokenLine="5" tokenColumn="7"/>
      </CLASS>
      <IDENTIFIER tokenValue="AClass" tokenLine="5" tokenColumn="13">
        <TOKEN tokenValue="AClass" tokenLine="5" tokenColumn="13"/>
      </IDENTIFIER>
      <classBody tokenValue="{" tokenLine="5" tokenColumn="20">
        <LWING tokenValue="{" tokenLine="5" tokenColumn="20">
          <TOKEN tokenValue="{" tokenLine="5" tokenColumn="20"/>
        </LWING>
        <RWING tokenValue="}" tokenLine="6" tokenColumn="0">
          <TOKEN tokenValue="}" tokenLine="6" tokenColumn="0"/>
        </RWING>
      </classBody>
    </classDeclaration>
  </typeDeclaration>
  <EOF tokenValue="" tokenLine="6" tokenColumn="1"/>
</compilationUnit>

提前谢谢你!

【问题讨论】:

    标签: xpath xpath-2.0


    【解决方案1】:

    我认为在 XPath 1.0 中没有一种与 2.0 版本一样通用的方法,您必须更加具体,例如检查前三个标记是否分别为another.pkg

    //importDeclaration/qualifiedIdentifier[*[1]/*/@tokenValue = 'another']
                                           [*[2]/*/@tokenValue = '.']
                                           [*[3]/*/@tokenValue = 'pkg']
    

    这比您的 2.0 版本稍微严格一些,要完全等效,您必须检查第三个标记 pkg 开头,而不是 等于 pkg(例如,允许 "another.pkgfoo",尽管这实际上可能并不理想)。

    【讨论】:

    • 谢谢,这很好用,但是我的字符串比我的例子长得多,这就是我选择 choroba 答案的原因。
    【解决方案2】:

    如果标记化始终相同,则只需连接前三个标记:

     //importDeclaration/qualifiedIdentifier[concat((.//*/*/@tokenValue)[1],
                                                    (.//*/*/@tokenValue)[2],
                                                    (.//*/*/@tokenValue)[3])='another.pkg']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2023-02-09
      • 2015-10-29
      • 1970-01-01
      相关资源
      最近更新 更多