Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace attributes in Builder gem

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!

like image 461
Luke Mueller Avatar asked May 10 '12 20:05

Luke Mueller


People also ask

Can a namespace have attribute?

An attribute with a namespace can be created using Element. setAttributeNS() . Note: an attribute does not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it has no namespace.

What is namespace attribute?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.


2 Answers

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/>
like image 153
Luke Mueller Avatar answered Sep 29 '22 10:09

Luke Mueller


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
like image 26
yesmeck Avatar answered Sep 29 '22 08:09

yesmeck