user1191140 user1191140 - 7 months ago 10
Ruby Question

Ruby XML Parsing

I have a sample XML document like

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchSecretsResponse xmlns="urn:thesecretserver.com">
<SearchSecretsResult>
<Errors/>
<SecretSummaries>
<SecretSummary>
<SecretId>86</SecretId>
<SecretName>hostName\root</SecretName>
<SecretTypeName>Unix Root Account (SSH)</SecretTypeName>
</SecretSummary>
</SecretSummaries>
</SearchSecretsResult>
</SearchSecretsResponse>
</soap:Body>
</soap:Envelope>


I am trying to parse it using Nokogiri. My code is

doc = Nokogiri::XML.parse(xml)
puts doc.xpath('//SecretSummary')


But this doesn't print anything. What am I doing wrong ?

Answer

You'll need to alias the namespace.

Nokogiri::XML(xml).xpath('//foo:SecretSummary', 'foo' =>  'urn:thesecretserver.com')
Comments