What is a multi branch xpath query for finding this?

时间秒杀一切 提交于 2020-02-08 11:36:05

问题


This is my current xpath query:

//node:Expr_Assign[subNode:var/node:Expr_Variable/subNode:Name/scalar:string='my_chinese_surname' and subNode:expr/node:Scalar_String/subNode:value/scalar:string='Qiu']

I'm trying to find it in this xml:

<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:attribute="http://nikic.github.com/PHPParser/XML/attribute" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
 <scalar:array>
  <node:Expr_Assign>
   <attribute:startLine>
    <scalar:int>2</scalar:int>
   </attribute:startLine>
   <attribute:endLine>
    <scalar:int>2</scalar:int>
   </attribute:endLine>
   <subNode:var>
    <node:Expr_Variable>
     <attribute:startLine>
      <scalar:int>2</scalar:int>
     </attribute:startLine>
     <attribute:endLine>
      <scalar:int>2</scalar:int>
     </attribute:endLine>
     <subNode:name>
      <scalar:string>my_chinese_surname</scalar:string>
     </subNode:name>
    </node:Expr_Variable>
   </subNode:var>
   <subNode:expr>
    <node:Scalar_String>
     <attribute:startLine>
      <scalar:int>2</scalar:int>
     </attribute:startLine>
     <attribute:endLine>
      <scalar:int>2</scalar:int>
     </attribute:endLine>
     <subNode:value>
      <scalar:string>Qiu</scalar:string>
     </subNode:value>
    </node:Scalar_String>
   </subNode:expr>
  </node:Expr_Assign>
 </scalar:array>
</AST>

It's not currently working. Infact I can't even select the scalar:array.

What's wrong with the query?

I figured it was namespaces. But I can't register them, the urls are dead? http://nikic.github.com/PHPParser/XML/node this doesn't lead anywhere.


回答1:


Should be:

//node:Expr_Assign[subNode:var/node:Expr_Variable/subNode:name/scalar:string='my_chinese_surname' and subNode:expr/node:Scalar_String/subNode:value/scalar:string='Qiu']

Note subNode:name vs subNode:Name

but might I recommend that an expression like this might be a little more readable although I can't tell if it would work exactly the way you want based on what the rest of your XML looks like

//node:Expr_Assign[.//scalar:string='my_chinese_surname'][ .//scalar:string='Qiu']

Also note that namespace URIs are not URLs, they don't need to be resolveable to be used. The URL format is commonly used for a namespace URI though because one usually owns the domain and thus is reasonably certain that others won't be creating namespace conflicts by using namespace URIs in that domain. A namespace URI could just as easily be a urn, uuid, or email address.



来源:https://stackoverflow.com/questions/13730539/what-is-a-multi-branch-xpath-query-for-finding-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!