Created
September 29, 2011 17:59
-
-
Save davidcelis/1251428 to your computer and use it in GitHub Desktop.
why is this wrong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
When I do xml.xpath("//<postdata>")
or xml.at_css("<postdata>")
, I get a Nokogiri::XML::XPath::SyntaxError
because of the angle brackets
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
it is caused by postdata's special namespace. use <postdata> and both the css and xpath statements work.