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!
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.
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.
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/>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With