-1

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.

CMCDragonkai
  • 6,222
  • 12
  • 56
  • 98

1 Answers1

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.

nine9ths
  • 796
  • 6
  • 15
  • Hey when I test your first line of xpath, it still doesn't work according to this: http://xpath.online-toolz.com/tools/xpath-editor.php I think I need to resolve the namespaces. How would that work? – CMCDragonkai Dec 06 '12 at 00:29
  • Oh it works now, even though it failed on an online tester. However the output is like this: 2222my_chinese_surname22Qiu Why is that? Are those 2s from the scalar:int? How can I determine what kind of output should I get? – CMCDragonkai Dec 06 '12 at 00:36
  • Actually, do you know what the operator is for a wildcard search on the value? Like at this part (scalar:string='my_chinese_surname') can it be (scalar:string=*)? What is the wildcard? – CMCDragonkai Dec 06 '12 at 01:19
  • If you want to match regardless of the content just remove the `=...` i.e. `//node:Expr_Assign[.//scalar:string][ .//scalar:string]` will succeed if those elements exist. If you want to make sure they have text you can do this: `//node:Expr_Assign[.//scalar:string/text()][ .//scalar:string/text()]` If you want to ensure they have non-whitespace text you can do this: `//node:Expr_Assign[.//scalar:string/text()[normalize-space()]][ .//scalar:string/text()[normalize-space()]]` – nine9ths Dec 06 '12 at 05:19