ایکس کوئیری مثال

ਇਸ ਸੈਕਸ਼ਨ ਵਿੱਚ, ਇੱਕ ਉਦਾਹਰਣ ਦੇ ਅਧਿਐਨ ਰਾਹੀਂ ਅਸੀਂ ਕੁਝ ਬੁਨਿਆਦੀ 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 ਚੁਣਨ ਲਈ bookstore ਐਲੀਮੈਂਟ, /book bookstore ਐਲੀਮੈਂਟ ਤੋਂ ਸਾਰੇ 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 ਡਾਕੂਮੈਂਟ ਤੋਂ ਹਾਸਲ ਕੀਤੇ ਗਏ ਡਾਟਾ ਨੂੰ ਸੀਮਤ ਕਰਨ ਲਈ ਕਲੇਮ ਵਰਤਦਾ ਹੈ。

ਹੇਠਾਂ ਦੇ ਕਲੇਮ ਨੂੰ bookstore ਐਲੀਮੈਂਟ ਤੋਂ ਸਾਰੇ 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>