Professional Documents
Culture Documents
XML Schema
XML Schema
XML Schema is an XML-based alternative to DTD. An XML schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD).
A Reference to a DTD
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "http://www.w3schools.com/dtd/note.dtd">
xmlns:xs="http://www.w3.org/2001/X MLSchema"
indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that namespace should be prefixed with xs:
xmlns="http://www.w3schools.com"
indicates that the default namespace is "http://www.w3schools.com".
elementFormDefault="qualified
indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
where xxx is the name of the element and yyyy is the data type of the element. XML Schema has a lot of built-in data types. The most common types are:
xs:string xs:decimal xs:integer xs:boolean xs:date xs:time
Example
Here are some XML elements:
<lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn>
A fixed value is also automatically assigned to the element, and you cannot specify another value. In the following example the fixed value is "red":
<xs:element name="color" type="xs:string" fixed="red"/>
A complex XML element, "description", which contains both elements and text:
<description> It happened on <date lang="norwegian">03.03.99</date> .... </description>
If you use the method described above, only the "employee" element can use the specified complex type.
2. The "employee" element can have a type attribute that refers to the name of the complex type to use:
<xs:element name="employee" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType>
If you use the method described above, several elements can refer to the same complex type, like this:
<xs:element name="employee" type="personinfo"/> <xs:element name="student" type="personinfo"/> <xs:element name="member" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType>
SAX
XML Parsers
What is an XML parser?
Software that reads and parses XML Passes data to the invoking application The application does something useful with the data
XML Parsers
Why is this a good thing?
Since XML is a standard, we can write generic programs to parse XML data Frees the programmer from writing a new parser each time a new data format comes along
XML Parsers
Two types of parser
SAX (Simple API for XML)
Event driven API Sends events to the application as the document is read
There are various implementations available Java implementations are provided as part of JAXP (Java API for XML Processing) JAXP is included as a package in Java 1.4
JAXP is available separately for Java 1.3
Unlike many XML technologies, SAX and DOM are relatively easy
Some DOM implementations have methods for changing the XML document in memory; SAX implementations do not
Callbacks
SAX works through callbacks: you call the parser, it calls methods that you supply
Your program
startDocument(...)
The SAX parser
main(...) parse(...)
startElement(...)
characters(...) endElement( ) endDocument( )
XQuery
XQuery
XQuery is to XML what SQL is to database tables. XQuery was designed to query XML data. Example
for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title
Path Expressions
XQuery uses path expressions to navigate through elements in an XML document. The following path expression is used to select all the title elements in the "books.xml" file: doc("books.xml")/bookstore/book/title
(/bookstore selects the bookstore element, /book selects all the book elements under the bookstore element, and /title selects all the title elements under each book element)
Predicates
XQuery uses predicates to limit the extracted data from XML documents. The following predicate is used to select all the book elements under the bookstore element that have a price element with a value that is less than 30:
The for clause selects all book elements under the bookstore element into a variable called $x. The where clause selects only book elements with a price element with a value greater than 30. The order by clause defines the sort-order. Will be sort by the title element. The return clause specifies what should be returned. Here it returns the title elements.