XQuery-HTML 형식

XQuery는 또한 XML 문서를 HTML 페이지로 변환하는 데 쉽게 사용할 수 있습니다. XQuery가 수행하는 방법을 이해하려면 다음 예제를 살펴보십시오.

동일한 books.xml 파일을 사용합니다. 다음 예제에서는 books.xml에서 XQuery 추출 데이터를 사용하고 모든 책의 제목과 해당 가격을 포함하는 HTML 테이블을 만듭니다.

books.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
   
   <book category="JAVA">
      <title lang="en">Learn Java in 24 Hours</title>
      <author>Robert</author>
      <year>2005</year>
      <price>30.00</price>
   </book>
   
   <book category="DOTNET">
      <title lang="en">Learn .Net in 24 hours</title>
      <author>Peter</author>
      <year>2011</year>
      <price>70.50</price>
   </book>
   
   <book category="XML">
      <title lang="en">Learn XQuery in 24 hours</title>
      <author>Robert</author>
      <author>Peter</author> 
      <year>2013</year>
      <price>50.00</price>
   </book>
   
   <book category="XML">
      <title lang="en">Learn XPath in 24 hours</title>
      <author>Jay Ban</author>
      <year>2010</year>
      <price>16.50</price>
   </book>
   
</books>

아래는 위의 XML 문서에서 실행될 Xquery 표현식입니다.

books.xqy

let $books := (doc("books.xml")/books/book) return <table><tr><th>Title</th><th>Price</th></tr> { for $x in $books order by $x/price
   return <tr><td>{data($x/title)}</td><td>{data($x/price)}</td></tr>
}
</table>
</results>

결과

<table>
   <tr>
      <th>Title</th>
      <th>Price</th>
   </tr>
   <tr>
      <td>Learn XPath in 24 hours</td>
      <td>16.50</td>
   </tr>   
   <tr>
      <td>Learn Java in 24 Hours</td>
      <td>30.00</td>
   </tr>
   <tr>
      <td>Learn XQuery in 24 hours</td>
      <td>50.00</td>
   </tr>   
   <tr>
      <td>Learn .Net in 24 hours</td>
      <td>70.50</td>
   </tr>
</table>

Verify Result

To verify the result, replace the contents of books.xqy (given in the Environment Setup chapter) with the above XQuery expression and execute the XQueryTester java program.

XQuery Expressions

Here we've used the following XQuery expressions −

  • data() function to evaluate the value of the title element, and

  • {} operator to tell the XQuery processor to consider data() as a function. If {} operator is not used, then data() will be treated as normal text.