XQuery 예제
예제를 보고 몇 가지 기본적인 XQuery를 배워봅시다.
XML 예제 문서
아래 예제에서는 다음 XML 문서를 사용합니다.
"books.xml":
<?xml version="1.0" encoding="UTF-8"?>
<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"에서 노드를 선택하는 방법은 무엇입니까?
기능
XQuery는 함수를 사용하여 XML 문서에서 데이터를 추출합니다.
doc() 함수는 "books.xml" 파일을 여는 데 사용됩니다.
doc("books.xml")
경로 표현식
XQuery는 경로 표현식을 사용하여 XML 문서의 요소를 탐색합니다.
다음 경로 표현식은 "books.xml" 파일의 모든 제목 요소를 선택하는 데 사용됩니다.
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 문서에서 추출된 데이터를 제한합니다.
다음 술어는 값이 30보다 작은 price 요소가 있는 bookstore 요소 아래의 모든 book 요소를 선택하는 데 사용됩니다.
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>