XML/XQuery/doc

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

doc() funciton in for loop

File: Data.xml
<prod:car xmlns:prod="http://wbex.ru/prod">
  <prod:id>0002</prod:id>
  <prod:name language="en">name 2</prod:name>
</prod:car>
File: Query.xquery
declare namespace cat = "http://wbex.ru/cat";
<report xmlns="http://wbex.ru/report"
           xmlns:prod="http://wbex.ru/prod"> {
  for $car in doc("Data.xml")/prod:car
  return <linecar>
           {$car/prod:id}
           {$car/prod:name}
         </linecar>
} </report>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<report xmlns:prod="http://wbex.ru/prod" xmlns="http://wbex.ru/report">
   <linecar>
      <prod:id>0002</prod:id>
      <prod:name language="en">name 2</prod:name>
   </linecar>
</report>



Load children

File: Data.xml

<order>
  <car dept="A">
    <number>1</number>
  </car>
  <car dept="B">
    <number>2</number>
  </car>
  <car dept="B">
    <number>3</number>
  </car>
  <car dept="C">
    <number>4</number>
  </car>
</order>

File: Query.xquery

doc("Data.xml")/order/car/number
Output:



Use doc() function to load xml docuCt

File: Data.xml

<order>
  <car dept="A">
    <number>1</number>
  </car>
  <car dept="B">
    <number>2</number>
  </car>
  <car dept="B">
    <number>3</number>
  </car>
  <car dept="C">
    <number>4</number>
  </car>
</order>

File: Query.xquery

doc("Data.xml")//number
Output:
<?xml version="1.0" encoding="UTF-8"?>
<number>1</number>
<number>2</number>
<number>3</number>
<number>4</number>



Use doc() function to load XML document

File: Data.xml

<?xml version="1.0"?>
<bib>
  <book year="1988">
    <author>
      <last>A</last>
      <first>B</first>
    </author>
  </book>
  <book year="2004">
    <author>
      <last>E</last>
      <first>F</first>
    </author>
  </book>
</bib>

File: Query.xquery
doc("Data.xml")/bib/book
Output:
<?xml version="1.0" encoding="UTF-8"?>
<book year="1988">
    <author>
         <last>A</last>
         <first>B</first>
    </author>
  </book>
<book year="2004">
    <author>
         <last>E</last>
         <first>F</first>
    </author>
  </book>