Using a namespace declaration in a constructor : namespace « XQuery « XML






Using a namespace declaration in a constructor


File: Data.xml


<order>
  <car model="A">
    <id>0001</id>
    <name language="en">name 1</name>
  </car>
  <car model="B">
    <id>0002</id>
    <name language="en">name 2</name>
  </car>
  <car model="B">
    <id>0003</id>
    <name language="en">name 3</name>
  </car>
  <car model="C">
    <id>0004</id>
    <name language="en">name 4</name>
  </car>
</order>


File: Query.xquery

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <xhtml:h1 class="carHdr">car order</xhtml:h1>
  <xhtml:ul>{
    for $prod in doc("Data.xml")/order/car
    return <xhtml:li class="{$prod/@model}">id: {
                               data($prod/id)}</xhtml:li>
  }</xhtml:ul>
</xhtml:html>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml">
   <xhtml:h1 class="carHdr">car order</xhtml:h1>
   <xhtml:ul>
      <xhtml:li class="A">id: 0001</xhtml:li>
      <xhtml:li class="B">id: 0002</xhtml:li>
      <xhtml:li class="B">id: 0003</xhtml:li>
      <xhtml:li class="C">id: 0004</xhtml:li>
   </xhtml:ul>
</xhtml:html>

 








Related examples in the same category

1.Querying with namespaces
2.Prefixed name tests
3.Namespace declarations in query different from input document
4.Using namespace declaration attributes
5.Namespace declaration impact on input elements
6.Using XML namespace declarations
7.Prolog namespace declarations