How to add attributes in XQuery Output

In this article we will discuss about how to add our own attributes to the output of the XQuery.
  • 2422

As we know that we can add our own elements to the output of the XQuery and in the same way we can also add attributes of the XML document  as the attributes in the HTML List. Suppose we want to use the name attribute as the color attribute in the HTML font tag  then we have to run the following XQuery against the following XML Document.

<?xml version="1.0" encoding="utf-8" ?>


  <color name="red">



  <color name="green">



  <color name="yellow">



  <color name="pink">




Now, from the above XML Document we will add the name attribute as the color attribute in the font tag in HTML . We will run the following XQuery.

for $x in doc("Colors.xml")/Colorshop/color
order by $x/id
return <font color="{data($x/@name)}">{data($x/id)}</font>

The output will be as follows.

<font color="red">C01</font>
<font color="green">C02</font>
<font color="yellow">C03</font>
<font color="pink">C04</font>

Here we have seen that the name attribute of the XML document is coming as the color attribute of the font tag in HTML.

Ask Your Question

Got a programming related question? You may want to post your question here

Programming Answers here


More Articles

© 2020 DotNetHeaven. All rights reserved.