Ruby to_xml with repeating same nodes - ruby

I would like to generate something like:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Test>
<Car>
<engine>A</engine>
<wheels>4</wheels>
</Car>
<Car>
<engine>B</engine>
<wheels>2</wheels>
</Car>
</Test>
but doing:
{"Car"=>[{"engine"=>"A", "wheels"=>"4"}, {"engine"=>"B", "wheels"=>"2"}]}.to_xml(:root => "Test")
returns:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Test>
<Car type=\"array\">
<Car>
<engine>A</engine>
<wheels>4</wheels>
</Car>
<Car>
<engine>B</engine>
<wheels>2</wheels>
</Car>
</Car>
</Test>
You see, I don't want the parent node "<Car type=\"array\">"
Any idea how to achieve this?
Thank you!

For this simple case you can use Array#to_xml like so
values = {"Car"=>[{"engine"=>"A", "wheels"=>"4"}, {"engine"=>"B","wheels"=>"2"}]}.values.pop
#=> [{"engine"=>"A", "wheels"=>"4"}, {"engine"=>"B", "wheels"=>"2"}]
values.to_xml(:root => "Test", skip_types: true, children: "Car")
#=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Test>\n <Car>\n <engine>A</engine>\n <wheels>4</wheels>\n </Car>\n <Car>\n <engine>B</engine>\n <wheels>2</wheels>\n </Car>\n</Test>\n"
So More Concisely
{"Car"=>[{"engine"=>"A", "wheels"=>"4"}, {"engine"=>"B", "wheels"=>"2"}]}.values.pop.to_xml(:root => "Test", skip_types: true, children: "Car")
Will Return
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Test>
<Car>
<engine>A</engine>
<wheels>4</wheels>
</Car>
<Car>
<engine>B</engine>
<wheels>2</wheels>
</Car>
</Test>
Array#to_xml allows you to pass in root and children options so you can name the root "Test" and the children "Car" as requested. If this was just an example and the case is more complex then there could be concerns with this in which case I would recommend looking at builder which allows you immense control over nodes and their naming conventions.

How about utilizing the :skip_types => true option?
{"Car"=>[{"engine"=>"A", "wheels"=>"4"}, {"engine"=>"B", "wheels"=>"2"}]}.to_xml(:root => "Test", :skip_types => true)

Related

Problem when trying to deploy Community Page on salesforce

I'm getting this error when I'm trying to deploy a community page to one sandbox. I pull the same thing from another sandbox.
These are my errors:
Here is my package.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>CommunityTemplateDefinition</name>
</types>
<types>
<members>Dealer_Community</members>
<name>CustomSite</name>
</types>
<types>
<members>Dealer Community</members>
<name>Network</name>
</types>
<types>
<members>Dealer_Community1</members>
<name>ExperienceBundle</name>
</types>
<version>55.0</version>
</Package>
And this is the folder that generates error with loginAppPageId:
{
"forgotPasswordRouteId" : null,
"isAvailableToGuests" : false,
"isFilteredComponentsView" : false,
"isProgressiveRenderingEnabled" : true,
"loginAppPageId" : null,
"mainAppPageId" : "2fe3359b-d766-429e-9d67-ec8afe81cc47",
"preferredDomain" : "none",
"selfRegistrationRouteId" : null,
"type" : "site"
}
Any ideea on this? Thanks!

WSO2 Payload Factory not working as expected

I am having troubles configuring the Payload Factory to make it work as expected.
I am trying to process the response of a backend service, which is:
<Documents xmlns="http://ws.wso2.org/dataservice">
<Document>
<Data>
{ "_id" : { "$oid" : "5bbce6ec9e0aae7e5c3a150a"} , "Plan" : "XXXX"}
</Data>
</Document>
<Document>
<Data>
{ "_id" : { "$oid" : "5bbce7279e0aae7e5c3a150b"} , "Plan" : "YYYY"}
</Data>
</Document>
</Documents>
I need to extract the json in each Data tag and construct a JSON that looks something like:
{
Data:
{
_id: {...},
Plan: ...
}
}
Just for testing purposes, I was trying to use the Payload Factory Mediator to get all Data tags using XPath. This is the outSequence of my API (again, the response that comes from the backend is just like the one above):
<outSequence>
<payloadFactory media-type="xml">
<format>
<newTestData>$1</newTestData>
</format>
<args>
<arg evaluator="xml" expression="//Data" />
</args>
</payloadFactory>
<log level="full" />
</outSequence>
The problem is that the log shows that the newTestData tag is empty after the Payload Factory process the response message.
The XPath was tested in an XPath Online Tester and it is correct so: what am I doing wrong?

How to select an element using Nokogiri

Given the following XML, I want to get the value "0123456" for Name="Cat":
xml.xpath '//Custom[Name="Cat"]'
Gives me the first custom, which is correct, but I only want the "Value" not the entire Custom node.
<body>
<Custom>
<count>1</count>
<Name>Cat</Name>
<Value>0123456</Value>
</Custom>
<Custom>
<count>2</count>
<Name>Dog</Name>
<Value>9876543</Value>
</Custom>
<body>
I only want the "Value" not the entire Custom node.
So just go on writing the path:
//Custom[Name="Cat"]/Value
I prefer to use CSS selectors over XPath, for readability, as usually CSS contains less visual noise:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<body>
<Custom>
<count>1</count>
<Name>Cat</Name>
<Value>0123456</Value>
</Custom>
<Custom>
<count>2</count>
<Name>Dog</Name>
<Value>9876543</Value>
</Custom>
<body>
EOT
foo = doc.search('name:contains("Cat")').map{ |node|
node.next_element.text
}
foo # => ["0123456"]
This works because Nokogiri contains some of the jQuery CSS extensions, resulting in some useful additions.
To get the value element text you need to set the xpath as below:
doc = Nokogiri::HTML(<<EOT)
<body>
<Custom>
<count>1</count>
<Name>Cat</Name>
<Value>0123456</Value>
</Custom>
<Custom>
<count>2</count>
<Name>Dog</Name>
<Value>9876543</Value>
</Custom>
<body>
EOT
val=doc.xpath("//Custom[Name='Cat']/Value").text()
val => "0123456"

Rails RSS builder with image, banner, logo

I tried to create a RSS feed with image for each article and eventually a banner for Feedly and a logo but I struggle to do that.
Here is the preview of what I get :
Here is my RSS Builder:
#encoding: UTF-8
xml.instruct! :xml, :version => "1.0"
xml.rss :version => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:media' => 'http://search.yahoo.com/mrss/' do
xml.channel do
xml.title "My RSS feed"
xml.description "Super description"
xml.link "MY_URL"
xml.language "en"
xml.tag! 'atom:link', :rel => 'self', :type => 'application/rss+xml', :href => "MY_URL/feed"
for article in #articles
xml.item do
xml.title article.name
xml.pubDate article.created_at.to_s(:rfc822)
xml.link "MY_URL"
xml.guid "MY_URL"
xml.media(:content, :url => article.image.url(:medium))
xml.media(:thumbnail, :url => article.image.url(:thumbnail))
xml.description "<p>" + article.description + "</p>"
end
end
end
end
And here is the result:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>My RSS feed</title>
<description>Super description</description>
<link>MY_URL</link>
<language>en</language>
<atom:link rel="self" type="application/rss+xml" href="MY_URL/feed"/>
<item>
<title>Dojo</title>
<pubDate>Mon, 23 Nov 2015 16:58:41 +0000</pubDate>
<link>MY_URL</link>
<guid>MY_URL</guid>
<media:content url="MY_URL"/>
<media:thumbnail url="MY_URL"/>
<description><p>Your security & privacy advisor.</p></description>
</item>
</channel>
</rss>
Thanks for your help with that!
I've checked out how it works in blog.feedly.com and here is example:
<item>
<title>POST_TITLE</title>
<link>URL_TO_POST</link>
<comments>URL_TO_COMMENTS</comments>
<pubDate>ISODate</pubDate>
<dc:creator>CDATA_AUTHOR_METATAG</dc:creator>
<category><![CDATA[All]]></category>
<guid isPermaLink="false">URL_WITHOUT_SLUG</guid>
<description>CDATA_DESCRIPTION</description>
<content:encoded>CDATA_POST_CONTENT</content:encoded>
<wfw:commentRss>URL_TO_COMMENTS_RSS</wfw:commentRss>
<slash:comments>NUMBER_OF_COMMENTS</slash:comments>
<media:content url="IMG_URL" medium="image">
<media:title type="html">Author</media:title>
</media:content>
<media:content url="IMG_URL" medium="image">
<media:title type="html">img_short_description</media:title>
</media:content>
</item>
Try to implement your feed items like I described above and if it will not work, check out item parent tags too. You need to do your rss feed just like at blog.feedly.com

Storing the value of attribute and matching it in Xquery

<a>
{
for $p in doc("x.xml")/user//district
where data($p/#population) div data($p/#area) > 20
return <b>
<c> {$p/#name/string()} </c>
<d> {$p/#population div $p/#area} </d>
<d> {$p/#city/string()}</e>
</b>
}
</a>
I am getting the id of the district but I want the name of the country . So i want to traverse back to the district from city. Like in snippet below, I am getting country(id = 'f0_149') but i am unable to match it and print the country name 'Austria'instead.
I am new to Xquery so I am unaware about many things.
XML FILE SNIPPET:
<?xml version="1.0" encoding="UTF-8"?>
<user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<country id='f0_149' name='Austria' capital='f0_1467' population='10000000' total_area='50000' >
<name>
Austria
</name>
<district id='f0_17440' name='Burgenland' country='f0_149' capital='f0_2291' population='250000' area='5000'>
<city id='f0_2291' country='f0_149' province='f0_17440'>
<name>
Eisenstadt
</name>
<population year='87'>
10102
</population>
</city>
You can use the parent:: XPath axis:
<c> { $p/parent::country/#name/string() } </c>

Resources