XML/XQuery/Axis

Материал из Web эксперт
Перейти к: навигация, поиск

Compare element value

File: Data.xml
<prices>
  <priceList>
    <prod>
      <price>29.99</price>
    </prod>
    <prod>
      <price>69.99</price>
    </prod>
  </priceList>
</prices>

File: Query.xquery
if (doc("Data.xml")/prices/priceList/prod[price < 30])
then <bin>{
       doc("Data.xml")/*/priceList/prod[price < 30]
     }</bin>
else ()

Output:
<?xml version="1.0" encoding="UTF-8"?>
<bin>
   <prod>
      <price>29.99</price>
    </prod>
</bin>



Embedded direct element constructors

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>
</order>

File: Query.xquery
<html>
  <h1>car order</h1>
  <p>A <i>huge</i> list of {count(doc("Data.xml")//car)} cars.</p>
</html>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<html>
   <h1>car order</h1>
   <p>A <i>huge</i> list of 3 cars.</p>
</html>



Enclosed expressions that evaluate to attributes

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>
</order>
File: Query.xquery
for $prod in doc("Data.xml")/order/car
return <li>{$prod/@model}id: {$prod/id}</li>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<li model="A">id: <id>0001</id>
</li>
<li model="B">id: <id>0002</id>
</li>
<li model="B">id: <id>0003</id>
</li>



Enclosed expressions that evaluate to elements

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>
</order>

File: Query.xquery
for $prod in doc("Data.xml")/order/car
return <li>id: {$prod/id}</li>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<li>id: <id>0001</id>
</li>
<li>id: <id>0002</id>
</li>
<li>id: <id>0003</id>
</li>



Enclosed expressions with multiple subexpressions

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>
</order>

File: Query.xquery
for $prod in doc("Data.xml")/order/car
return <li>{$prod/@model,"string",5+3,$prod/id}</li>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<li model="A">string 8<id>0001</id>
</li>
<li model="B">string 8<id>0002</id>
</li>
<li model="B">string 8<id>0003</id>
</li>



Including complex elements from the input document

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>
</order>

File: Query.xquery
for $prod in doc("Data.xml")/order/car[@model = "A"]
return $prod

Output:
<?xml version="1.0" encoding="UTF-8"?>
<car model="A">
    <id>0001</id>
    <name language="en">name 1</name>
  </car>



Including elements from the input document

File: Data.xml

<order>
  <car model="A">
    <name language="en">name 1</name>
  </car>
  <car model="B">
    <name language="en">name 2</name>
  </car>
  <car model="B">
    <name language="en">name 3</name>
  </car>
</order>
File: Query.xquery
for $prod in doc("Data.xml")/order/car[@model = "A"]
return $prod/name

Output:
<?xml version="1.0" encoding="UTF-8"?>
<name language="en">name 1</name>