XQuery 实例

在本节,让我们通过研究一个例子来学习一些基础的 XQuery 语法。

XML 实例文档

我们将在下面的例子中使用这个 XML 文档。

"books.xml" :

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>
</bookstore>

మీ బ్రౌజర్‌లో "books.xml" ఫైల్ని చూడండి.

ఎలా "books.xml" నుండి నోడ్స్ ఎంపిక చేస్తాము?

ఫంక్షన్

XQuery ఫంక్షన్లను ఉపయోగిస్తుంది XML డాక్యుమెంట్లో డేటాను పొందడానికి.

doc() ద్వారా "books.xml" ఫైల్ని తెరుస్తుంది:

doc("books.xml")

పాథ్ ఎక్స్‌ప్రెషన్

XQuery XML డాక్యుమెంట్లో మెన్యుల ద్వారా పాథ్ ఎక్స్‌ప్రెషన్ ద్వారా పరిభ్రాంతి చేస్తుంది.

కింది పాథ్ ఎక్స్‌ప్రెషన్ "books.xml" ఫైల్లోని అన్ని title మెన్యులను ఎంపిక చేస్తుంది:

doc("books.xml")/bookstore/book/title

(/bookstore బుక్‌స్టోర్ మెన్యును ఎంపిక చేస్తుంది, /book బుక్‌స్టోర్ మెన్యులోని అన్ని book మెన్యులను ఎంపిక చేస్తుంది, మరియు /title ప్రతి book మెన్యులోని అన్ని title మెన్యులను ఎంపిక చేస్తుంది)

పైని XQuery ద్వారా కింది డేటాను పొందవచ్చు:

<title lang="en">Everyday Italian</title>
<title lang="en">Harry Potter</title>
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

విధానం

XQuery XML డాక్యుమెంట్ నుండి పొందే డేటాను విధానం ద్వారా నియంత్రిస్తుంది.

కింది విధానం బుక్‌స్టోర్ మెన్యూలోని అన్ని book మెన్యూలను ఎంపిక చేస్తుంది మరియు ఎంపికచేసిన book మెన్యులోని price మెన్యుల విలువ కన్నా 30 కంటే తక్కువగా ఉండాలి:

doc("books.xml")/bookstore/book[price<30]

పైని XQuery ద్వారా కింది డేటాను పొందవచ్చు:

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>