XML/XQuery/Axis

Материал из Web эксперт
Версия от 11:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Compare element value

   <source lang="xml">

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>

</source>
   
  


Embedded direct element constructors

   <source lang="xml">

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>

car order

A huge list of {count(doc("Data.xml")//car)} cars.

</html>

Output: <?xml version="1.0" encoding="UTF-8"?> <html>

car order

A huge list of 3 cars.

</html>

</source>
   
  


Enclosed expressions that evaluate to attributes

   <source lang="xml">

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
  • {$prod/@model}id: {$prod/id}
  • Output: <?xml version="1.0" encoding="UTF-8"?>

  • id: <id>0001</id>
  • id: <id>0002</id>
  • id: <id>0003</id>
  • </source>
       
      
    


    Enclosed expressions that evaluate to elements

       <source lang="xml">
    

    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
  • id: {$prod/id}
  • Output: <?xml version="1.0" encoding="UTF-8"?>

  • id: <id>0001</id>
  • id: <id>0002</id>
  • id: <id>0003</id>
  • </source>
       
      
    


    Enclosed expressions with multiple subexpressions

       <source lang="xml">
    

    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
  • {$prod/@model,"string",5+3,$prod/id}
  • Output: <?xml version="1.0" encoding="UTF-8"?>

  • string 8<id>0001</id>
  • string 8<id>0002</id>
  • string 8<id>0003</id>
  • </source>
       
      
    


    Including complex elements from the input document

       <source lang="xml">
    

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


    Including elements from the input document

       <source lang="xml">
    

    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>

    </source>