I am working on a requirement where I need to return element name and value as Key value pair as below using XQUERY.
[code=123,px_last=value attribute of first data,last_update=value attribute of 2nd data and so on]
There are 7 data element with attribute value inside should be read as above with first field mapped to first data and second mapped to second data value attribute. etc..
With your help I was able to generate the output but got struck where I need to map the first field element to first data attribute value and so on etc.
Thanks in advance
XML file:
<root>
<fields>
<field>PX_LAST</field>
<field>LAST_UPDATE</field>
<field>LAST_UPDATE_DT</field>
<field>SECURITY_DES</field>
<field>FUT_CUR_GEN_TICKER</field>
<field>YLD_CNV_BID</field>
<field>YLD_CNV_ASK</field>
</fields>
<Datas>
<Data>
<code>0</code>
<ins>
<id>CT30</id>
<key>Govt</key>
<ins/>
<data value="98.843750"/>
<data value="16:14:45">
</data>
<data value="06/03/2014"/>
<data value="T 3 3/8 05/15/44"/>
<data value=""/>
<data value="3.439"/>
<data value="3.437"/>
</Data>
<Data>
<code>0</code>
<ins>
<id>US0001W</id>
<key>Index</key>
<ins/>
<data value=".119000"/>
<data value="06:46"/>
<data value="06/03/2014"/>
<data value="ICE LIBOR USD 1 Week"/>
<data value=""/>
<data value="N.A."/>
<datavalue=".11900"></data>
</Data>
</Datas>
</root>
XQuery:
declare function xf:strip-namespace($e as element())
as element()
{
element { xs:QName(local-name($e)) }
{
for $child in $e/(@*,node())
return
if ($child instance of element())
then
xf:strip-namespace($child)
else
$child
}
};
let $nl := " "
let $count := 0
for $x in doc("test.xml")/soap:Envelope/soap:Body/dlws:retrieveGetDataResponse/dlws:instrumentDatas//*
let $y:=xf:strip-namespace($x)
return
if($y/name() = 'instrumentData')
then
concat($nl,'[','')
else if($y/name()='data')
then
concat($y/name(),'=',$y/data(@value),',')
else if($y/name() != 'instrument')
then
concat($y/name(),'=',$y/text(),',')
else
()
Output right now:
[code=123,data=werr,data="qwe",data="wer",......,] [code=456,data=rty,data="tyuu",data="uuu",......,]