Skip to content

Instantly share code, notes, and snippets.

@davidcelis
Created September 29, 2011 17:59
Show Gist options
  • Save davidcelis/1251428 to your computer and use it in GitHub Desktop.
Save davidcelis/1251428 to your computer and use it in GitHub Desktop.
why is this wrong
require "nokogiri"
xml = Nokogiri::XML(File.open("some_shit.xml"))
xml.at_css("xmldata")
# => nil
xml.xpath("//soap12:Envelope")
# => works, returns element and children
xml.xpath("//soap12:Envelope//soap12:Body")
# => works, returns element and children
xml.xpath("//soap12:Envelope//soap12:Body//postdata")
# => suddenly does not work, won't detect the <postdata> element
<?xml version="1.0" encoding="utf-8" ?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<cAuthentication xmlns="http://ar.masstech-pts.org/">
<UserName>username</UserName>
<Password>password1</Password>
</cAuthentication>
</soap12:Header>
<soap12:Body>
<postdata xmlns="http://ar.masstech-pts.org/">
<xmldata></xmldata>
</postdata>
</soap12:Body>
</soap12:Envelope>
@donpdonp
Copy link

it is caused by postdata's special namespace. use <postdata> and both the css and xpath statements work.

@davidcelis
Copy link
Author

When I do xml.xpath("//<postdata>") or xml.at_css("<postdata>"), I get a Nokogiri::XML::XPath::SyntaxError because of the angle brackets

@donpdonp
Copy link

the original query is correct, im saying the element 'postdata' in the XML is defining its own namespace which is putting it out of reach of the normal query. i dont understand xml namespaces that well but if postdata has to have a namespace, perhaps use a namespace prefix, such as how soap12 works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment