Create a XML file having a particular schema using Xquery - xquery

I am new to Xquery, I want to change the given xml into another xml format.
Given XML:
<?xml version="1.0" encoding="UTF-8"?>
<Store>
<consumer id="H01">
<name>John Doe</name>
<items>
<item type = "Torch">
<price>$3</price>
</item>
<item type = "Gas">
<price>$4</price>
</item>
</items>
</consumer >
<consumer id="H05">
<name>Jane Doe</name>
<items>
<item type = "Cell">
<price>$8</price>
</item>
<item type = "Shirt">
<price>$12</price>
</item>
</items>
</consumer>
Desired XML Format:
<Store>
<user>
<number><id>H01</id><name>John Doe</name></number>
<number><id>H05</id><name>Jane Doe</name></number>
</user>
<inventory>
<number><type>Torch</type><price>$3</price></number>
<number><type>Gas</type><price>$4</price></number>
<number><type>Cell</type><price>$8</price></number>
<number><type>Shirt</type><price>$12</price></number>
</inventory>
</Store>
Xquery I made:
for $customer in distinct-values(doc("../xml/store.xml")/store/consumer/#id)
let $name := doc("../xml/store.xml")/store/consumer[#id=$customer]/name
for $object in distinct- values(doc("../xml/store.xml")/store/consumer[#id=$customer]/items/item/#type)
return
<store>
<user>
<number>
<id>{$customer}</id>
{$name}
</number>
</user>
<inventory>
<number>
<type>{$object}</type>
</number>
</inventory>
</store>
Where exactly am I going wrong? Is there a way we could make attributes as new node elements.

your two for clauses are nested, you don't want that in this case. I would do something like this:
let $doc := doc("../xml/store.xml")
let $customerIds := distinct-values($doc/store/consumer/#id)
return
<store>
<user>{for $customerId in $customerIds
let $consumer := $doc/store/consumer[#id=$customerId]
return <number><id>{data($consumer/#id)}</id><name>{$consumer/name}</name></number>
}
</user>
<inventory>
similar thing for items
</inventory>
</store>
Is there a way we could make attributes as new node elements.
Yes you can for example use the data() function to extract the text. See example above.

Related

XQuery produces incorrect output

The example xml looks like this:
<sales>
............
<customer custid="108">
<name>NORTH WOODS HEALTH AND FITNESS SUPPLY CENTER</name>
<address>98 LONE PINE WAY</address>
<city>HIBBING</city>
<state>MN</state>
<zip>55649</zip>
<area>612</area>
<phone>566-9123</phone>
<repid>7844</repid>
<creditlimit>8000</creditlimit>
<ord ordid="613">
<orderdate>1987-02-01</orderdate>
<shipdate>1987-02-01</shipdate>
<total>6400</total>
<item itemid="1">
<product_ref ref="100871"/>
<actualprice>5.6</actualprice>
<qty>100</qty>
<itemtot>560</itemtot>
</item>
</ord>
</customer>
<product prodid="100860">
<descrip>ACE TENNIS RACKET I</descrip>
<price>
<stdprice>35</stdprice>
<minprice>28</minprice>
<startdate>1986-06-01</startdate>
</price>
</product>
.........
</sales>
let $product := doc('sales.xml')/sales/product
for $item in doc('sales.xml')/sales/customer/ord/item
where $item/actualprice < $product[#prodid=$item/product_ref/#ref]/price/minprice
return $item
And I get as result this:
<item itemid="1">
<product_ref ref="100861"/>
<actualprice>35</actualprice>
<qty>1</qty>
<itemtot>35</itemtot>
</item>
<item itemid="3">
<product_ref ref="101863"/>
<actualprice>10</actualprice>
<qty>150</qty>
<itemtot>1500</itemtot>
</item>
<item itemid="7">
<product_ref ref="101863"/>
<actualprice>12.5</actualprice>
<qty>200</qty>
<itemtot>2500</itemtot>
</item>
<item itemid="5">
<product_ref ref="101863"/>
<actualprice>9</actualprice>
<qty>100</qty>
<itemtot>900</itemtot>
</item>
But the items with ref=101863 are not right. Only the item with id=5 is less then minprice.
Why does this error occur? Tried a lot of different queries but it gives me always the same result.
It works fine with ref=100861.
Assuming the query isn't schema-aware, you are comparing the two prices as strings, not as numbers. You need to convert both to numbers before comparison.

XQuery: Return value of attribute if it contains another spesific attribute

I want to return a result on all the books which contain the "databases" attribute in inside of "field".
for $e in //testbook[field="databases"]
return $e/title
Sample of the .xml file:
<?xml version="1.0" encoding="UTF-?>
<testbook>
<book>
<author> AuthorGuy </author>
<title> theBook</title>
<field> databases </field>
</book>
</testbook>
There are two issues:
<title/> and <field/> elements are contained in a <book/> element, and are not direct children of <testbook/>, fix the path expressions.
The <field/> tag contains the field wrapped in whitespace, use contains($string, $needle) instead of a simple comparison.
A working example:
let $document := document{<testbook>
<book>
<author> AuthorGuy </author>
<title> theBook</title>
<field> databases </field>
</book>
</testbook>}
for $e in $document//testbook[contains(book/field, "databases")]
return $e/book/title

BizTalk Mapper: Link between siblings

I have the following XML that I need to transform:
<?xml version="1.0" encoding="utf-8"?>
<TestRecords>
<TestData>
<Users>
<User>
<Id>BG123</Id>
<Name>Bill Gates</Name>
</User>
<User>
<Id>SN123</Id>
<Name>Satya Nadella</Name>
</User>
</Users>
<UserDetails>
<UserDetail>
<UserId>SN123</UserId>
<CompanyName>Microsoft Corp</CompanyName>
</UserDetail>
<UserDetail>
<UserId>
<UserId>BG123</UserId>
<CompanyName>Bill Gates Foundation</CompanyName>
</UserId>
</UserDetail>
</UserDetails>
I need to map this XML into the following XML:
<?xml version="1.0" encoding="utf-8"?>
<TestRecords>
<TestData>
<Users>
<User>
<Id>BG123</Id>
<Name>Bill Gates</Name>
<CompanyName>Bill Gates Foundation</CompanyName>
</User>
<User>
<Id>SN123</Id>
<Name>Satya Nadella</Name>
<CompanyName>Microsoft Corp</CompanyName>
</User>
</Users>
</TestData>
</TestRecords>
When I loop over Users/User, I need to find the UserDetail where UserDetail/UserId is equal to the current User/Id
Thank you and best regards
Michael
If you don't want to do Custom XSLT as suggested by FCR the only other option when you have different looping structures is to have an intermediate schema and two maps.
Which produces
<TestRecords>
<TestData>
<Users>
<User>
<Id>BG123</Id>
<Name>Bill Gates</Name>
<UserDetails>
<UserID>SN123</UserID>
<CompanyName>Microsoft Corp</CompanyName>
</UserDetails>
<UserDetails>
<UserID>BG123</UserID>
<CompanyName>Bill Gates Foundation</CompanyName>
</UserDetails>
</User>
<User>
<Id>SN123</Id>
<Name>Satya Nadella</Name>
<UserDetails>
<UserID>SN123</UserID>
<CompanyName>Microsoft Corp</CompanyName>
</UserDetails>
<UserDetails>
<UserID>BG123</UserID>
<CompanyName>Bill Gates Foundation</CompanyName>
</UserDetails>
</User>
</Users>
</TestData>
</TestRecords>
Which you can then run through this second map to produce the desired outcome.
This will become very inefficient however if the second list is large.
This is a common lookup pattern in xslt and there is also the opportunity to use xsl:key to create an index which can boost performance on large documents. Refer here if you need to convert a .btm to xslt.
(Also, I'm assuming that there isn't a double wrapper UserId at on the last UserDetails/UserDetail element):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="userLookup"
match="/TestRecords/TestData/UserDetails/UserDetail" use="UserId"/>
<!--identity template - copy everything by default -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!--i.e.match only Users in the first Users/User tree. Actually the explicit
ancestor qualifier is redundant because of the other suppress template -->
<xsl:template match="User[ancestor::Users]">
<User>
<xsl:copy-of select="child::*" />
<CompanyName>
<xsl:value-of select="key('userLookup', Id)/CompanyName"/>
</CompanyName>
</User>
</xsl:template>
<!--Suppress the second userdetails part of the tree entirely -->
<xsl:template match="UserDetails" />
</xsl:stylesheet>
Fiddle here

Flex e4x filter out children

I'm trying to filter my xml so that the childnodes are not displayed.
This xml will then be used as a dataprovider for my advanceddatagrid.
Given this XML :
<item text="folder1" display="true">
<item text="folder2" display="true">
<item text="node" display="true">
<item display="false">
<property1>val1</property1>
<property2>val2</property2>
</item>
</item>
</item>
</item>
What I want is an XML with only the nodes that have the property display set to true.
So, the resulting XML should be:
<item text="folder1" display="true">
<item text="folder2" display="true">
<item text="node" display="true">
</item>
</item>
</item>
When I try trace(data.item.(#display == 'true')); every node is still displayed, even the ones with display false..
Any help would be appreciated ..
okay, this is how I solved it now :
var childNodes:XMLList = new XMLList(data.descendants("item").(#display == 'false'));
for ( var i:int = childNodes.length() - 1; i >= 0; i-- ) {
delete childNodes[i];
}
I think this is happening because of the format of that XML.
All of the item elements under folder1 are child nodes of folder1. Since it has
display="true", then the trace will display all of its children.
Anyone else, is this XML formatted correctly to do the search?

Voicexml how to store input into a global variable

I'm creating a voicexml appliacation.
I want to store an user input into a global variable.
I wondered, the input should be stored in the fieldvar. shouldn't it? After I tried it with this, i tried to store it in an global variable:
<assign name="myvar" expr="'myinput'"/>
but somehow it didn't work. I used value expr="var" as expr.
<?xml version="1.0" encoding="UTF-8"?>
<vxml xmlns="http://www.w3.org/2001/vxml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/vxml
http://www.w3.org/TR/voicexml20/vxml.xsd"
version="2.0">
<var name="myProdukt" />
<form id="test">
<field name="var">
<prompt bargein="true" bargeintype="hotword" >Sagen Sie ein Produkt</prompt>
<grammar root="main" version="1.0" xml:lang="de-DE">
<rule id="main" scope="public">
<one-of>
<item> p1 </item>
<item> p2 </item>
<item> p3 </item>
<item> p4 </item>
</one-of>
</rule>
</grammar>
<filled>
<assign name="myProdukt" expr="<value expr="var"/>"/>
</filled>
</field>
</form>
<<!--[...] Here i want to use the input.-->
</vxml>
thanks in advance
---------------EDIT:
now i used this:
<filled>
test
<assign name="myProdukt" expr="var" />
</filled>
I only changed that. The Applications says "test" but then there is an error.
It isn'T allowed to use "var" instead I used an other name :-)
Did you try a simple assignment of field var to the variable myProdukt like so ?
<filled>
<assign name="myProdukt" expr="var"/>
</filled>
Which would be fine except that according to Section 5.1, Variables and Expressions of the Voice XML specification:
VoiceXML variables, including form
item variables, must not contain
ECMAScript reserved words.
So, you'll need to rename the field var to something that is not a reserved word in ECMAscript, say productSelection:
<field name="productSelection">
<!-- .. prompt, grammar as before .. -->
<filled>
<assign name="myProdukt" expr="productSelection"/>
</filled>
</field>

Resources