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.
  • 2381

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" ?>

<Colorshop>

  <color name="red">

    <id>C01</id>

  </color>

  <color name="green">

    <id>C02</id>

  </color>

  <color name="yellow">

    <id>C03</id>

  </color>

  <color name="pink">

    <id>C04</id>

  </color>

</Colorshop>

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.

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

The output will be as follows.

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

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.