I am using xml_edit to change the value of xml. This is not working with below scenario.
I want to change the title of Atwood, Margaret author in Second Bookshelf in below xml:
<?xml version="1.0"?>
<bookshelf>
<Name>First Bookshelf</Name>
<book>
<author>Conway, Damien</author>
<title>Perl Best Practices</title>
</book>
<book>
<author>Atwood, Margaret</author>
<title>Perl Best Practices</title>
</book>
</bookshelf>
<bookshelf>
<Name>Second Bookshelf</Name>
<book>
<author>Conway, Damien</author>
<title>Perl Best Practices</title>
</book>
<book>
<author>Atwood, Margaret</author>
<title>Perl Best Practices</title>
</book>
</bookshelf>
I am trying to use below recipe. I do't know, how could I verify the Name and auther of second bookshelf.
xml_edit 'change title' do
path 'C:\\testing\\text.xml'
target '/bookshelf/book/title[text()=\'Perl Best Practices\']'
fragment '<title>Hello World</title>'
action :replace
end
It looks like your xml is not valid
It should look like:
<?xml version="1.0"?>
<shelves>
<bookshelf>
<Name>First Bookshelf</Name>
<book>
<author>Conway, Damien</author>
<title>Perl Best Practices</title>
</book>
<book>
<author>Atwood, Margaret</author>
<title>Perl Best Practices</title>
</book>
</bookshelf>
<bookshelf>
<Name>Second Bookshelf</Name>
<book>
<author>Conway, Damien</author>
<title>Perl Best Practices</title>
</book>
<book>
<author>Atwood, Margaret</author>
<title>Perl Best Practices</title>
</book>
</bookshelf>
</shelves>
With that in mind you can do:
xml_edit 'change title' do
path 'C:\\testing\\text.xml'
target '/shelves/bookshelf[Name[text()=\'Second Bookshelf\']]/book[author[text()=\'Atwood, Margaret\']]/title'
fragment '<title>Hello World</title>'
action :replace
end
Related
I have a XML like
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>Vaidyanathan Nagarajan</author>
</book>
<book category="it" cover="paperback">
<title lang="en">Learning XML</title>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
When I evaluate the XPATH
/bookstore/book/author/parent::book
I get the result:
/bookstore[1]/book[1]
/bookstore[1]/book[2]
But as the second has three -nodes I am searching for a XPATH expression that gives me back the following:
/bookstore[1]/book[1]
/bookstore[1]/book[2]
/bookstore[1]/book[2]
/bookstore[1]/book[2]
Is this possible?
Thanks
Regards
Mario
I got a solution, the XPATH
//author!parent::book/path()
returns:
/Q{}bookstore[1]/Q{}book[2]
/Q{}bookstore[1]/Q{}book[2]
/Q{}bookstore[1]/Q{}book[2]
/Q{}bookstore[1]/Q{}book[2]
Thanks
Regards
Mario
This is the XML input:
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
I am using BaseX 7.9. When I request attributes,
for $book in collection()/bookstore/book
return $book/#*
an error occurs:
[SENR0001] Attributes cannot be serialized: attribute category {"COOKING"}.
How can this be fixed? Thanks for helping me!
The XQuery 3.1 Serialization specification provides the new "adaptive" serialization mode, which allows the serialization of attribute and namespace nodes. Since Version 8.0 of BaseX, this mode is used as new default.
This was different in earlier versions of the specification, which did not allow attributes to be output on their own (see the error code SENR0001 for more information).
I have the following code for generating a xml from a rabl template:
obj = OpenStruct.new
obj.categories = [{node: ["Foo","Bar"]},{node: ["Test1","Test2"]}]
Rabl::Renderer.xml(obj, 'adapter_xml')
and this is the rabl template adapter_xml.rabl
object #obj => :root
attributes :categories
which generates this XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<categories>
<category>
<node>
<node>Foo</node>
<node>Bar</node>
</node>
</category>
<category>
<node>
<node>Test1</node>
<node>Test2</node>
</node>
</category>
</categories>
</root>
But what I want to achieve is the following format, without the extra <node> tags:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<categories>
<category>
<node>Foo</node>
<node>Bar</node>
</category>
<category>
<node>Test1</node>
<node>Test2</node>
</category>
</categories>
</root>
Is there any way to do this with rabl? Or do I have to modify the ruby code mentioned first?
This is the structure of the file I need to import.
<channel>
<item>
<type>image</type>
<title>title image</title>
<id>1</id>
<image_url>url_to_image</image_url>
</item>
<item>
<type>page</type>
<title>node title</title>
<id>2</id>
<ref>
<entity>image_ref</entity>
<ref_value>1</ref_value>
</ref>
<ref>
<entity>category</entity>
<ref_value>5</ref_value>
</ref>
</item>
</channel>
In the page item the tag contains the id of the image item.
How do I add the image url from the image item to the page item?
I'm trying to use
/channel/item[id=ref/ref_value[../entity/text() = 'image_ref']]/image_url but it does not work...
What's the XPath expression to not import the image item but just the page item?
Thanks in advance
Use:
/*/item[type='image' and id=../item[type='page']
/ref[entity = 'image_ref']/ref_value]
/image_url/text()
XSLT - based verification:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/item[type='image' and id=../item[type='page']
/ref[entity = 'image_ref']/ref_value]
/image_url/text()"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied to the provided XML document:
<channel>
<item>
<type>image</type>
<title>title image</title>
<id>1</id>
<image_url>url_to_image</image_url>
</item>
<item>
<type>page</type>
<title>node title</title>
<id>2</id>
<ref>
<entity>image_ref</entity>
<ref_value>1</ref_value>
</ref>
<ref>
<entity>category</entity>
<ref_value>5</ref_value>
</ref>
</item>
</channel>
the XPath expression is evaluated and the result of this evaluation is copied to the output:
url_to_image
Update:
The OP has implied in comments that there may be many "page items" and "image items" and that he needs an expression, getting the image url for only a specific page.
This XPath expression:
/*/item[type='image'
and id=../item[type='page'][1]
/ref[entity = 'image_ref']/ref_value
]
/image_url/text()"/>
produces the wanted image url for the first "page item" in the following XML document:
<channel>
<item>
<type>image</type>
<title>title image</title>
<id>1</id>
<image_url>url_to_image</image_url>
</item>
<item>
<type>image</type>
<title>title image</title>
<id>2</id>
<image_url>url2_to_image</image_url>
</item>
<item>
<type>page</type>
<title>node title</title>
<id>3</id>
<ref>
<entity>image_ref</entity>
<ref_value>1</ref_value>
</ref>
<ref>
<entity>category</entity>
<ref_value>5</ref_value>
</ref>
</item>
<item>
<type>page</type>
<title>node title</title>
<id>4</id>
<ref>
<entity>image_ref</entity>
<ref_value>2</ref_value>
</ref>
<ref>
<entity>category</entity>
<ref_value>5</ref_value>
</ref>
</item>
</channel>
The result produced is:
url_to_image
To get the wanted url for the second page item, we simply modify the above XPath expression to:
/*/item[type='image'
and id=../item[type='page'][2]
/ref[entity = 'image_ref']/ref_value
]
/image_url/text()"/>
and now the result is:
url2_to_image
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://extranet.mcs.be/DEV_QUALITY_API/modules/quality/services/soap/quality.php"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getQuestionnairesListResponse>
<return SOAP-ENC:arrayType="ns1:QuestionnaireListItem[34]"
xsi:type="ns1:ArrayOfQuestionnaireListItem">
<item xsi:type="ns1:QuestionnaireListItem">
<ID xsi:type="xsd:string">0000000022</ID>
<Code xsi:type="xsd:string">Interest PubTransp</Code>
<Reference xsi:type="xsd:string">Check Employees Interest in Public
Transport</Reference>
</item>
<item xsi:type="ns1:QuestionnaireListItem">
<ID xsi:type="xsd:string">0000000008</ID>
<Code xsi:type="xsd:string">CS SRE North 2003</Code>
<Reference xsi:type="xsd:string">Customer Satisfaction SRE North 2003</Reference>
</item>
<item xsi:type="ns1:QuestionnaireListItem">
<ID xsi:type="xsd:string">0000000006</ID>
<Code xsi:type="xsd:string">CS SRE South 2003</Code>
<Reference xsi:type="xsd:string">Customer Satisfaction SRE South 2003</Reference>
</item>
.
.
.
I want to parse the above soap String (I actually want to get items from the above soap). How could I do this?
There's a gem called Savon that's specifically made for dealing with SOAP in Ruby.
There's good documentation on the web site, once you looked into that and have more specific questions I'm sure we can help you.