Saturday, August 25, 2012

XML Document



<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
     <!-- a bookstore database -->
     <book isbn=“111111” cat=“fiction”>
          <!-- a particular book -->
          <title lang=“chn”>Harry Potter</title>
          <price unit=“us”>79.99</price>
     </book>
     <book isbn=“222222” cat=“textbook”>
           <title lang=“eng”>Learning XML</title>
           <price unit=“us”>69.95</price>
     </book>
           <book isbn "333333" cat "textbook">
           <title lang="eng">Intro. to Databases</title>
           <price unit="usd">39.00</price>
     </book>
</bookstore>
DTD
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE bookstore [
  <!ELEMENT bookstore (book*)>
<!ELEMENT book (title, price)>
<!ATTLIST book isbn ID #REQUIRED
                 cat CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
  <!ATTLIST title lang CDATA #REQUIRED>
<!ELEMENT price (#PCDATA)>
  <!ATTLIST price unit CDATA #REQUIRED>
]>


Exercise 1
1) Find the title and isbn of non-fiction books with a price of more than 50 USD.
              –   /bookstore/book[attribute::cat!="fiction" and price>50.00]/title |
                  /bookstore/book[attribute::cat!="fiction" and price>50.00]/@isbn
2) Find average price of textbooks.
– sum(/bookstore/book[attribute::cat="textbook"]/price/number(text()))
div
                   count(/bookstore/book[attribute::cat textbook ]/price)3) Find the titles of textbooks on XML.
             – /bookstore/book[attribute::cat="textbook" and contains(title,
 "XML")]/title/text()

Exercise 2
1) Create a new document which contain only the isbn and title of textbooks.
<textbooks>
{ for $book in doc("bookstore.xml")//book
   where $book/@cat="textbook"
   return <textbook isbn="$book/@isbn">{$book/title}</textbook>
}
</textbooks>
Result:
<textbooks>
<textbook isbn="222222">
<title lang="eng">Learning XML</title>
</textbook>
<textbook isbn="333333">
<title lang="eng">Intro. to Databases</title>
</textbook>
</textbooks>
2) Find the title and price of the book with isbn = '222222'.
for $book in doc("bookstore.xml")//book
where $book[@isbn="222222"]
return <book>{ $book/title, $book/price}</book>
Result:
<book>
<title lang="eng">Learning XML</title>
<price unit="usd">69.95</price>
</book>

3) Produce a list of non-fictions with their title and price, sorted by price.
<nonfiction‐list>
{ for $book in doc("bookstore.xml")//book, $title in $book/title, $price
in $book/price
   where $book/@cat!="fiction"   order by $price/text()
   return <nonfiction>{ $title, $price}</nonfiction>
}
</nonfiction‐list>
Result:
<nonfiction‐list>
<nonfiction>
<title lang="eng">Intro. to Databases</title>
<price unit="usd">39.00</price>
</nonfiction>
<nonfiction>
<title lang="eng">Learning XML</title>
<price unit="usd">69.95</price>
</nonfiction>
</nonfiction‐list>

4) Find the title of the text book with highest price.
<textbooks>
{ let $prices := doc("bookstore.xml")//book[@cat="textbook"]/price
   let $max := max($prices)
   return
<max‐price‐textbook price="{$max}">
{for $book in doc("bookstore.xml")//book
  where $book/price = $max
  return $book/title
}
</max‐price‐textbook>
}
</textbooks>
Result:
<textbooks>
<max‐price‐textbook price="69.95">
<title lang="eng">Learning XML</title>
</max‐price‐textbook>
</textbooks>

5) Organize books by categories.
<summary‐by‐category>
{ let $categories :=
for $category in doc("bookstore.xml")//book/@cat
    return $category
for $cat in distinct‐values($categories)
return
<category id="{$cat}">{ for $book in doc("bookstore.xml")//book
where $book[@cat = $cat]
return $book }
</category>
}
</summary‐by‐category>
6) Reconstruct the document to produce the total price and count of books in each
category.
<price‐by‐category>
{ let $categories :=
for $category in doc("bookstore.xml")//book/@cat
return $category
for $cat in distinct‐values($categories)

return
<category id="{$cat}">
    { let $prices‐in‐cat := doc("bookstore.xml")//book[@cat=$cat]/price
return <price total="{sum($prices‐in‐cat)}"
count="{count($prices‐in‐cat)}"/>
     }
</category>
}
</price‐by‐category>






XML Quiz


You can test your XML skills with W3Schools' Quiz.

The Test

The test contains 20 questions and there is no time limit.
The test is not official, it's just a nice way to see how much you know, or don't know, about XML.

Count Your Score

You will get 1 point for each correct answer. At the end of the Quiz, your total score will be displayed. Maximum score is 20 points.
Good luck! Start the XML Quiz

XML Examples


These examples demonstrate XML files, XML formatting and XML transformation (XSLT).
They also demonstrate JavaScript used together with XML (AJAX).

Viewing XML Files


XML and CSS


XML and XSLT


Parsing XML and the XML DOM


XML to HTML


XML Applications


XML Output From a Server


XML DOM Advanced

XML Summary


XML Summary

XML can be used to exchange, share, and store data.
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
XML has very simple syntax rules. XML with correct syntax is "Well Formed". Valid XML also validates against a DTD.

XSLT is used to transform XML into other formats like HTML.
All modern browsers have a built-in XML parser that can read and manipulate XML.
The DOM (Document Object Model) defines a standard way for accessing XML.
The XMLHttpRequest object provides a way to communicate with a server after a web page has loaded.

XML Namespaces provide a method to avoid element name conflicts.
Text inside a CDATA section is ignored by the parser.
Our XML examples also represent a summary of this XML tutorial.

What to Study Next?

Our recommendation is to learn about the XML DOM and XSLT.

If you want to learn more about validating XML, we recommend DTD and XML Schema.
Below is a short description of each subject.

XML DOM (Document Object Model)

The XML DOM defines a standard way for accessing and manipulating XML documents.
The XML DOM is platform and language independent and can be used by any programming language like Java, JavaScript, and VBScript.

If you want to learn more about the DOM, please visit our XML DOM tutorial.

XSLT (XML Stylesheet Language Transformations)

XSLT is the style sheet language for XML files.

With XSLT you can transform XML documents into other formats, like XHTML.

If you want to learn more about XSLT, please visit our XSLT tutorial.

XML DTD (Document Type Definition)

The purpose of a DTD is to define what elements, attributes and entities is legal in an XML document.
With DTD, each of your XML files can carry a description of its own format with it.

DTD can be used to verify that the data you receive, and your own data, is valid.
If you want to learn more about DTD, please visit our DTD tutorial.

XML Schema

XML Schema is an XML based alternative to DTD.

Unlike DTD, XML Schemas has support for datatypes, and XML Schema use XML Syntax.
If you want to learn more about XML Schema, please visit our XML Schema tutorial.