Namespace Attributes in Stone Builder

I am trying to create this sample in a Ruby on Rails application with a gem builder :

<?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 cannot get the attribute inside this tag.

I would have thought that

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

will work, but it is not.

Please, help!

+4
source share
3 answers

It turned 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/> 
+6
source

From the Builder doc: http://builder.rubyforge.org/

Some support for XML namespaces is now available. If the first argument to invoke the tag is a character, it will be connected to the tag to create a namespace: a combination of tags. This is easier to show than to describe it.

xml.SOAP: Envelope do ... end

Just put a space before the colon in the namespace to create the correct form for the builder (for example, "SOAP: Envelope" => "xml.SOAP: Envelope")

So you can write like this:

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

This syntax can be used instead of xml.tag!('gp:contactGet')

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

Also, if you need namespaces for tags inside a block, you can use the following syntax. I could not find this information elsewhere, 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> 
+1
source

Source: https://habr.com/ru/post/1411862/


All Articles