<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML xmlns="http://www.w3.org/TR/REC-html40" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word"><head><META content="text/html; charset=us-ascii" http-equiv="Content-Type">

<META content="text/html; charset=us-ascii" http-equiv=Content-Type>
<META content="MSHTML 6.00.6000.16809" name=GENERATOR>
<STYLE>@font-face {
        font-family: Calibri;
}
@page Section1 {size: 8.5in 11.0in; margin: 1.0in 1.0in 1.0in 1.0in; }
P.MsoNormal {
        FONT-SIZE: 11pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Calibri","sans-serif"
}
LI.MsoNormal {
        FONT-SIZE: 11pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Calibri","sans-serif"
}
DIV.MsoNormal {
        FONT-SIZE: 11pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Calibri","sans-serif"
}
A:link {
        COLOR: blue; TEXT-DECORATION: underline; mso-style-priority: 99
}
SPAN.MsoHyperlink {
        COLOR: blue; TEXT-DECORATION: underline; mso-style-priority: 99
}
A:visited {
        COLOR: purple; TEXT-DECORATION: underline; mso-style-priority: 99
}
SPAN.MsoHyperlinkFollowed {
        COLOR: purple; TEXT-DECORATION: underline; mso-style-priority: 99
}
SPAN.EmailStyle17 {
        COLOR: windowtext; FONT-FAMILY: "Calibri","sans-serif"; mso-style-type: personal-compose
}
.MsoChpDefault {
        mso-style-type: export-only
}
DIV.Section1 {
        page: Section1
}
</STYLE>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]--></head><BODY>
<DIV>
<DIV align=left dir=ltr><FONT size=2>
<P class=MsoNormal><SPAN class=399303006-17022009><FONT color=#0000ff>Hi,</FONT></SPAN></P>
<P class=MsoNormal><SPAN class=399303006-17022009><FONT color=#0000ff></FONT></SPAN>&nbsp;</P>
<P class=MsoNormal><SPAN class=399303006-17022009><FONT color=#0000ff>How about 
something like this:</FONT></SPAN></P>
<P class=MsoNormal><SPAN class=399303006-17022009><FONT color=#0000ff></FONT></SPAN>&nbsp;</P>
<P class=MsoNormal><FONT color=#0000ff>let $x:= 
doc("source.xml")<o:p></o:p></FONT></P>
<P class=MsoNormal><FONT color=#0000ff>for $comp in 
$x/Unit/Data/Organization<o:p></o:p></FONT></P>
<P class=MsoNormal><FONT color=#0000ff>return<o:p></o:p></FONT></P>
<P class=MsoNormal><FONT color=#0000ff>&lt;Unit&gt;<o:p></o:p></FONT></P>
<P class=MsoNormal><FONT color=#0000ff>&lt;Company&gt;</FONT></P>
<P class=MsoNormal><o:p><SPAN class=399303006-17022009><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#ff0000>{$comp/@*}</FONT></SPAN></o:p></P>
<P class=MsoNormal><FONT color=#0000ff><SPAN class=399303006-17022009>&nbsp;&nbsp;&nbsp; 
</SPAN>&lt;/Detail&gt;&lt;/Detail&gt;<o:p></o:p></FONT></P>
<P class=MsoNormal><FONT color=#0000ff>&lt;/Company&gt;<o:p></o:p></FONT></P>
<P class=MsoNormal><FONT color=#0000ff>&lt;/Unit&gt;<o:p></o:p></FONT></P></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2></FONT><FONT color=#0000ff face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=399303006-17022009><FONT color=#0000ff face=Arial>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&nbsp;Company element.</FONT></SPAN></DIV>
<DIV><SPAN class=399303006-17022009><FONT color=#0000ff face=Arial></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=399303006-17022009><FONT color=#0000ff face=Arial>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.</FONT></SPAN></DIV>
<DIV><SPAN class=399303006-17022009><FONT color=#0000ff face=Arial></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=399303006-17022009><FONT color=#0000ff face=Arial>Kind 
regards,</FONT></SPAN></DIV>
<DIV><SPAN class=399303006-17022009><FONT color=#0000ff face=Arial>Geert</FONT></SPAN></DIV>
<DIV><FONT color=#0000ff face=Arial size=2></FONT><BR></DIV>
<BLOCKQUOTE style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
  <DIV align=left class=OutlookMessageHeader dir=ltr lang=en-us>
  </DIV></BLOCKQUOTE></DIV>
<DIV>&nbsp;&nbsp; 
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="MsoTableGrid" STYLE="BORDER-COLLAPSE: collapse; mso-yfti-tbllook: 480; mso-padding-alt: 0in 5.4pt 0in 5.4pt">
<TBODY>
<TR STYLE="HEIGHT: 49.25pt; mso-yfti-irow: 0; mso-yfti-firstrow: yes">
<TD STYLE="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 149.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; HEIGHT: 49.25pt" VALIGN="top" WIDTH="199">
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><B STYLE="mso-bidi-font-weight: normal"><SPAN STYLE="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-ansi-language: EN-US"></SPAN></B>&nbsp;</P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><B STYLE="mso-bidi-font-weight: normal"><SPAN STYLE="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-ansi-language: EN-US"><FONT SIZE="1">Drs. G.P.H. Josten</FONT></SPAN></B></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN STYLE="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana; mso-ansi-language: EN-US"><FONT SIZE="1"><EM>Consultant</EM></FONT></SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt">&nbsp;</P></TD>
<TD STYLE="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 315pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; HEIGHT: 49.25pt" VALIGN="top" WIDTH="420">
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN STYLE="FONT-SIZE: 8pt; FONT-FAMILY: Bembo; mso-ansi-language: EN-US">&nbsp;</SPAN></P></TD></TR>
<TR STYLE="HEIGHT: 98.75pt; mso-yfti-irow: 1">
<TD STYLE="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 149.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; HEIGHT: 98.75pt" VALIGN="top" WIDTH="199"><SPAN LANG="NL" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"><A HREF="http://www.daidalos.nl/"></A>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><B STYLE="mso-bidi-font-weight: normal"><SPAN LANG="EN-GB" STYLE="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB"><FONT SIZE="1">Daidalos BV</FONT></SPAN></B></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="EN-GB" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB"><EM><FONT SIZE="1">Source of Innovation</FONT></EM></SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="EN-GB" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB"><FONT SIZE="1">Hoekeindsehof 1-4</FONT></SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="EN-GB" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB"><FONT SIZE="1">2665<SPAN STYLE="mso-spacerun: yes">&nbsp; </SPAN>JZ<SPAN STYLE="mso-spacerun: yes">&nbsp; </SPAN>Bleiswijk</FONT></SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><FONT SIZE="1"><SPAN LANG="EN-GB" STYLE="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB"><FONT SIZE="1">Tel.:</FONT></SPAN><SPAN LANG="EN-GB" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB"> <FONT SIZE="1">+31</FONT> <FONT SIZE="1">(0) 10 850 1200</FONT></SPAN></FONT></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><FONT SIZE="1"><SPAN STYLE="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Verdana; mso-ansi-language: EN-US"><FONT SIZE="1">Fax:</FONT> </SPAN><SPAN STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-US">+<FONT SIZE="1">31 (0) 10 850 119</FONT></SPAN></FONT><SPAN LANG="NL" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"><FONT SIZE="1">9</FONT></SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="NL" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"><A HREF="http://www.daidalos.nl/"><FONT SIZE="1">www.daidalos.nl</FONT></A></SPAN></P><SPAN LANG="NL" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana">
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="NL" STYLE="FONT-SIZE: 10pt; FONT-FAMILY: Verdana"><FONT SIZE="1">KvK 27164984</FONT></SPAN></P></SPAN><SPAN LANG="NL" STYLE="FONT-SIZE: 8pt; FONT-FAMILY: Bembo"></SPAN></SPAN><SPAN LANG="NL" STYLE="FONT-SIZE: 8pt; FONT-FAMILY: Bembo"></SPAN></TD>
<TD STYLE="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 315pt; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; HEIGHT: 98.75pt" VALIGN="top" WIDTH="420">
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><FONT SIZE="1"><B STYLE="mso-bidi-font-weight: normal"><SPAN LANG="EN-GB" STYLE="COLOR: blue; FONT-FAMILY: Verdana; mso-ansi-language: EN-GB; mso-bidi-font-family: Arial"></SPAN></B><SPAN LANG="NL" STYLE="FONT-SIZE: 8pt; FONT-FAMILY: Bembo"></SPAN></FONT></P></TD></TR>
<TR STYLE="HEIGHT: 74pt; mso-yfti-irow: 2; mso-yfti-lastrow: yes">
<TD COLSPAN="2" STYLE="BORDER-RIGHT: #d4d0c8; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 6.45in; PADDING-TOP: 0in; BORDER-BOTTOM: #d4d0c8; HEIGHT: 74pt" VALIGN="top" WIDTH="619">
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="NL" STYLE="FONT-SIZE: 8pt; COLOR: #999999; FONT-FAMILY: Verdana"><FONT SIZE="1">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.</FONT></SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="NL" STYLE="FONT-SIZE: 8pt; FONT-FAMILY: Bembo">&nbsp;</SPAN></P>
<P CLASS="MsoNormal" STYLE="MARGIN: 0in 0in 0pt"><SPAN LANG="NL" STYLE="FONT-SIZE: 8pt; FONT-FAMILY: Bembo"><BLOCKQUOTE style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px"><DIV align=left class=OutlookMessageHeader dir=ltr lang=en-us>
  <FONT face=Tahoma size=2><B>From:</B> talk-bounces@x-query.com 
  [mailto:talk-bounces@x-query.com] <B>On Behalf Of </B>Diraviam 
  H<BR><B>Sent:</B> dinsdag 17 februari 2009 7:15<BR><B>To:</B> 
  talk@x-query.com<BR><B>Subject:</B> [xquery-talk] How to add an optional 
  attiribute to a constructed XML tag with XQuery ?<BR></FONT><BR></DIV>
  <DIV></DIV>
  <DIV class=Section1>
  <P class=MsoNormal>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.<o:p></o:p></P>
  <P class=MsoNormal>I am trying something like this, where the attribute "name" 
  is optional in source.<o:p></o:p></P>
  <P class=MsoNormal><o:p>&nbsp;</o:p></P>
  <P class=MsoNormal>let $x:= doc("source.xml")<o:p></o:p></P>
  <P class=MsoNormal>for $comp in $x/Unit/Data/Organization<o:p></o:p></P>
  <P class=MsoNormal>return<o:p></o:p></P>
  <P class=MsoNormal>&lt;Unit&gt;<o:p></o:p></P>
  <P class=MsoNormal>&lt;Company type="{$comp/@type}" primary="{$org/@primary}" 
  {if(fn:exists($x/Unit/Data/Organization/@name)) then name="{$org/@name}" else 
  "" } &gt;<o:p></o:p></P>
  <P class=MsoNormal>&lt;/Detail&gt;&lt;/Detail&gt;<o:p></o:p></P>
  <P class=MsoNormal>&lt;/Company&gt;<o:p></o:p></P>
  <P class=MsoNormal>&lt;/Unit&gt;<o:p></o:p></P></DIV>
  <TABLE>
    <TBODY>
    <TR>
      <TD bgColor=#ffffff><FONT color=#000000>DISCLAIMER:<BR>-----------------------------------------------------------------------------------------------------------------------<BR><BR>The 
        contents of this e-mail and any attachment(s) are confidential and 
        intended for the named recipient(s) only.<BR>It shall not attach any 
        liability on the originator or HCL or its affiliates. Any views or 
        opinions presented in <BR>this email are solely those of the author and 
        may not necessarily reflect the opinions of HCL or its 
        affiliates.<BR>Any form of reproduction, dissemination, copying, 
        disclosure, modification, distribution and / or publication of <BR>this 
        message without the prior written consent of the author of this e-mail 
        is strictly prohibited. If you have <BR>received this email in error 
        please delete it and notify the sender immediately. Before opening any 
        mail and <BR>attachments please check them for viruses and 
        defect.<BR><BR>-----------------------------------------------------------------------------------------------------------------------<BR></FONT></TD></TR></TBODY></TABLE></BLOCKQUOTE></SPAN></P></TD></TR></TBODY></TABLE></DIV>
<DIV>&nbsp;</DIV></BODY></HTML>