Error when get attributes with xquery in basex 7.9 - xpath

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).

Related

Create application/atom+xml result

I have an API on GoogleCloud and need to define an endpoint which returns a feed usable in Excel PowerPivot and Power BI. I have found this library using feed.ToAtom(), but the created feed does not work in PoverPivot, I get the following error: The payload kind 'Value' of the given data feed is not supported. but I can't find any information.
How can I create the needed result?
Here a snipped of the current result:
<?xml version="1.0" encoding="UTF-8"?>
<feed
xmlns="http://www.w3.org/2005/Atom">
<title>dfhfdhsfdh.net blog</title>
<id>http://dfhfdhsfdh.net/blojhlkjluizg</id>
<updated>2022-05-19T07:51:07+02:00</updated>
<rights>sdfsdf</rights>
<subtitle>discussion about tech, footie, photos</subtitle>
<link href="http://dfhfdhsfdh.net/blojhlkjluizg"></link>
<author>
<name>ergsrdg sdfgsfdg</name>
<email>dfhfdhsfdh#dfhfdhsfdh.net</email>
</author>
<entry>
<title>22906201</title>
<updated>2022-05-19T07:51:07+02:00</updated>
<id>http://dfhfdhsfdh.net/sdfsdfblog</id>
<link href="http://dfhfdhsfdh.net/blfghjghjog" rel="alternate"></link>
<summary type="html"></summary>
</entry>
<entry>
<title>22906197</title>
<updated>2022-05-19T07:51:07+02:00</updated>
<id>http://dfhfdhsfdh.net/sdfsdfblog</id>
<link href="http://dfhfdhsfdh.net/blfghjghjog" rel="alternate"></link>
<summary type="html"></summary>
</entry>
</feed>
I have also created the result by my self, the result is the following, but still get that error:
<?xml version="1.0" encoding="UTF-8"?>
<feed
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://www.w3.org/2005/Atom">
<title type="text">Guest Requests</title>
<id>http://localhost:4444/requests/GuestRequests/</id>
<updated>2022-05-19T09:33:40+02:00</updated>
<entry>
<id>http://localhost:4444/requests/22906201</id>
<title type="text">22906201</title>
<updated>2022-05-19T09:33:40+02:00</updated>
<author>
<name>TODO</name>
</author>
<content type="application/xml">
<m:properties>
<d:id>22906201</d:id>
<d:title>22906201</d:title>
</m:properties>
</content>
</entry>
</feed>

Replace the value in xml using Chef

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

Give back parents per children

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

Xpath add a field from an item to another item with a reference id

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

How to parse SOAP XML in ruby?

<?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.

Resources