You are on page 1of 22

Xlink & Xpointer,

XQuery
 XLink - XML Linking Language
 XLink berguna untuk membuat hyperlinks dalam
XML
 Setiap element dalam XML dapat menjadi
sebagai link
 XLink mendukung link sederhana (seperti HTML) dan

link extended (untuk menghubungkan beberapa


sumber sekaligus)
 Dengan XLink, link yang ada dapat di definisikan di

luar file yang terhubung


Link dalam HTML
<html>
<head>
</head>
<body>
<a href="www.google.com"> Link ke Google
Search</a>
</body>
</html>
Link dengan XLin
<?xml version="1.0"?>

<homepages xmlns:xlink="http://www.w3.org/1999/xlink">

<homepage xlink:type="simple"
xlink:href="http://www.w3schools.com">Visit W3Schools</homepage>

<homepage xlink:type="simple"
xlink:href="http://www.w3.org">Visit W3C</homepage>

</homepages>

Perhatikan, lagi-lagi disini di gunakan namespace untuk


mendeklarasikan Xlink yaitu dengan xmlns:xlink, dan diatas contoh
Xlink bertipe simple
XPointer

 XPointer XML Pointer Language


 XPointer memungkinkan links untuk
menunjuk (point) ke bagian spesifik dari XML
 XPointer menggunakan ekspresi XPath
untuk navigasi dalam XML
Anchor - Pointer dalam HTML
<html>
<head>
</head>
<body>
<H1>Table of Contents</H1> <P>
<A href="#section1">Pendahuluan</A><BR>
<A href="#section2">BAB I</A><BR>
<A href="#section2.1">I.I</A><BR>
<H2>
<A name="section1">Introduction</A>
</H2> …Detil Intro disini …
<H2>
<A name="section2">…isi BAB I … </A>
</H2>
<H3>
<A name="section2.1">…isi I.I … </A>
</H3>
</body>
</html>
Pointer dengan XPointer
<?xml version="1.0" encoding="ISO-8859-1"?>

<dogbreeds>

<dog breed="Rottweiler" id="Rottweiler">


  <picture url="http://dog.com/rottweiler.gif" />
  <history>The Rottweiler's ancestors were probably Roman
  drover dogs.....</history>
  <temperament>Confident, bold, alert and imposing, the Rottweiler
  is a popular choice for its ability to protect....</temperament>
</dog>

<dog breed="FCRetriever" id="FCRetriever">


  <picture url="http://dog.com/fcretriever.gif" />
  <history>One of the earliest uses of retrieving dogs was to
  help fishermen retrieve fish from the water....</history>
  <temperament>The flat-coated retriever is a sweet, exuberant,
  lively dog that loves to play and retrieve....</temperament>
</dog>

</dogbreeds>
<?xml version="1.0" encoding="ISO-8859-1"?>

<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">

<mydog xlink:type="simple"
  xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">
  <description xlink:type="simple"
  xlink:href="http://myweb.com/mydogs/anton.gif">
  Anton is my favorite dog. He has won a lot of.....
  </description>
</mydog>

<mydog xlink:type="simple"
  xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">
  <description xlink:type="simple"
  xlink:href="http://myweb.com/mydogs/pluto.gif">
  Pluto is the sweetest dog on earth......
  </description>
</mydog>

</mydogs>
XQuery
 XQuery di ciptakan untuk mengambil data dari XML
for $x in doc("books.xml")/bookstore/book
where $x/price>30dimana harga nya > 30 (dollar)
order by $x/title urutkan oleh judul
return $x/title kembalikan value title

$x = doc("books.xml")/bookstore/book
$x/price = doc("books.xml")/bookstore/book/price
XML Referensi
<?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>
 doc("books.xml")/bookstore/book/title mengembalikan:
<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>

 doc("books.xml")/bookstore/book[price<30]
mengembalikan:
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
FLOWR

 Bunga?
 Bukan tuh:
FLWOR adalah singkatan dari "For, Let,
Where, Order by, Return
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{$x}</li>
}
</ul>

Akan mengembalikan element dan data sbb (di browser/parser):

<ul>
<li><title lang="en">Everyday Italian</title></li>
<li><title lang="en">Harry Potter</title></li>
<li><title lang="en">Learning XML</title></li>
<li><title lang="en">XQuery Kick Start</title></li>
</ul>
<ul>
{
for $x in doc("books.xml")/bookstore/book/title
order by $x
return <li>{data($x)}</li>
}
</ul>

Akan mengembalikan hanya data/value dari sebuah element sbb (di browser/parser):

<ul>
<li>Everyday Italian</li>
<li>Harry Potter</li>
<li>Learning XML</li>
<li>XQuery Kick Start</li>
</ul>
Xquery & Xpath?

 Xquery nodes sama dengan Xpath nodes (apa


saja itu?)
 Xquery relationship juga sama dengan Xpath
relationship
Good Xquery?

 XQuery case-sensitive
 XQuery elements, attributes, and variables
harus XML names yang valid
 XQuery value text dapat menggunaan kutip
atau kutip satu
 Variabel XQuery didefinisikan menggunakan
$ diikuti oleh nama contoh $nilai1
 comments dipisahkn oleh “(:” dan “:)”,
contoh “(: ini adalah comment :)”
Kondisional?

for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
then <child>{data($x/title)}</child>
else <adult>{data($x/title)}</adult>

Mari kita baca bersama… 


Hasil if-then-else

<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>
Pemakaian dengan HTML
Xquery powerfull!

<html>
<body>

<h1>Bookstore</h1>

<ul>
{
for $x in doc("books.xml")/bookstore/book
order by $x/title
return <li>{data($x/title)}. Category: {data($x/@category)}</li>
}
</ul>

</body>
</html>

Hasilnya  …
Hasil render html +XQuery
<html>
<body>

<h1>Bookstore</h1>

<ul>
<li>Everyday Italian. Category: COOKING</li>
<li>Harry Potter. Category: CHILDREN</li>
<li>Learning XML. Category: WEB</li>
<li>XQuery Kick Start. Category: WEB</li>
</ul>

</body>
</html>

Hebat ya 
Lagi dong…

for $x at $i in doc("books.xml")/bookstore/book/title
return <book>{$i}. {data($x)}</book>

<book>1. Everyday Italian</book>


<book>2. Harry Potter</book>
<book>3. XQuery Kick Start</book>
<book>4. Learning XML</book>
To be continued…

 Kita lanjutkan di praktikum ok 

You might also like