[xquery-talk] How to add an optional attiribute to a constructed XML tag with XQuery ?

Geert Josten Geert.Josten at daidalos.nl
Tue Feb 17 07:34:02 PST 2009


Hi,

How about something like this:

let $x:= doc("source.xml")
for $comp in $x/Unit/Data/Organization
return
<Unit>
<Company>
    {$comp/@*}
    </Detail></Detail>
</Company>
</Unit>

You can also name the attributes explicitly, wrap them in an if and a for loop if you like. But make sure to insert attributes before anything else within the contents of the Company element.

I am not sure what $org was referring to, but if you simply want to copy the attributes of the Organization element to the Company element, then the above should work.

Kind regards,
Geert



Drs. G.P.H. Josten
Consultant




<http://www.daidalos.nl/>
Daidalos BV
Source of Innovation
Hoekeindsehof 1-4
2665  JZ  Bleiswijk
Tel.: +31 (0) 10 850 1200
Fax: +31 (0) 10 850 1199
www.daidalos.nl<http://www.daidalos.nl/>
KvK 27164984


De informatie - verzonden in of met dit emailbericht - is afkomstig van Daidalos BV en is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht onbedoeld hebt ontvangen, verzoeken wij u het te verwijderen. Aan dit bericht kunnen geen rechten worden ontleend.

From: talk-bounces at x-query.com [mailto:talk-bounces at x-query.com] On Behalf Of Diraviam H
Sent: dinsdag 17 februari 2009 7:15
To: talk at x-query.com
Subject: [xquery-talk] How to add an optional attiribute to a constructed XML tag with XQuery ?

I am creating an XML file say A, from another xml file B. The content of file B will be changing and some tags can have optional attributes. I want to include the attribute in the constructed XML file tag(s) through Xquery, only when it is available in source file B.
I am trying something like this, where the attribute "name" is optional in source.

let $x:= doc("source.xml")
for $comp in $x/Unit/Data/Organization
return
<Unit>
<Company type="{$comp/@type}" primary="{$org/@primary}" {if(fn:exists($x/Unit/Data/Organization/@name)) then name="{$org/@name}" else "" } >
</Detail></Detail>
</Company>
</Unit>
DISCLAIMER:
-----------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
It shall not attach any liability on the originator or HCL or its affiliates. Any views or opinions presented in
this email are solely those of the author and may not necessarily reflect the opinions of HCL or its affiliates.
Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of
this message without the prior written consent of the author of this e-mail is strictly prohibited. If you have
received this email in error please delete it and notify the sender immediately. Before opening any mail and
attachments please check them for viruses and defect.

-----------------------------------------------------------------------------------------------------------------------




-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://x-query.com/pipermail/talk/attachments/20090217/7be09981/attachment.htm


More information about the talk mailing list