6

Im trying to build out this sample in a Ruby on Rails app with the builder gem:

<?xml version="1.0" encoding="utf-8"?> 
<ngp:contactGet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ngp="http://www.ngpsoftware.com/ngpapi"> 
<campaignID>1033</campaignID> 
<contactID>199434</contactID> 
</ngp:contactGet>

I can generate a tag with a namespace as follows:

xml = Builder::XmlMarkup.new
xml.ngp :contactGet

...but I can't get an attribute inside that tag.

I would think that

xml.ngp :contactGet("xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance" "xmlns:ngp" =>"http://www.ngpsoftware.com/ngpapi"

would work but it doesn't.

Please Help!

3 Answers 3

8

Figured it out:

xml.tag!('gp:contactGet', {"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", "xmlns:ngp"=>"http://www.ngpsoftware.com/ngpapi"}) do 
  xml.campaignID("1033")
  xml.contactID("199434")
end

Produces...

<?xml version="1.0" encoding="UTF-8"?>
<gp:contactGet xmlns:ngp="http://www.ngpsoftware.com/ngpapi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <campaignID>1033</campaignID>
  <contactID>199434</contactID>
</gp:contactGet><to_s/>
Sign up to request clarification or add additional context in comments.

Comments

2

From Builder's doc: http://builder.rubyforge.org/

Some support for XML namespaces is now available. If the first argument to a tag call is a symbol, it will be joined to the tag to produce a namespace:tag combination. It is easier to show this than describe it.

xml.SOAP :Envelope do ... end

Just put a space before the colon in a namespace to produce the right form for builder (e.g. "SOAP:Envelope" => "xml.SOAP :Envelope")

So, you can write like this:

xml.gp :contactGet do
  xml.campaignID("1033")
  xml.contactID("199434")
end

Comments

2

You can use this syntax instead of xml.tag!('gp:contactGet')

xml.gp :contactGet do 
  xml.contactID, "199434"
end

Also if you need namespaces for tags within the block, you can use the following syntax. I could not find this information anywhere else so adding it here.

xml.tag!('gp:contactGet') do 
  xml.gp :contactID, "199434"
end

To give the following markup:

<gp:contactGet>
  <gp:contactID>199434</gp:contactID>
</gp:contactGet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.