问题
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