Hi there!
Today we continued our journey into the XML realm, by exploring the XML Schema, XPath, XSL and XML DB.
XML Schemas
XML Schemas are based on XML language and define and validate the structure of an XML document.
They can define simple and complex types, declare elements and attributes.
Why would we want to use these XML Schemas? Well, by using XML Schemas we can unify the modeling of document and data, validate XML documents and use user defined data types. And what is more, they are reusable.
Referencing the XML Schema from within the XML document simply enables the XML Schema definition.
One can, for example, create validation rules for an XML document; using the following type definition and tying these to an element definitions
<xsd:simpleType name="empid"> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxInclusive value="1000"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="employee_id" type="empid"/>
will create a type called “empid”, that must be an integer with a minimum value of 1, and cannot be greater than 1000. When the XML document contains an employee_id of 2500, the document will not be validated.
There is a whole bunch of other possibilities with XML Schemas, like complexTypes, sequences, choices, attributes, etc. but I will not go into that much detail now (Hmmm, are you sure you understood all of that, Arnoud? đ ).
XPath
Now, XPath was a nice subject. XPath provides in addressing the nodes (elements/values) of an XML document. It uses a very strong, though compact syntax to form expressions in URI and XML attribute values. XPath is designed to be used in XML applications like XSLT and XPointer (which we didn’t cover, btw).
The most basic XPath expression:
/departments/department/department_id
will return
<department_id>10</department_id>
from
<?xml version="1.0"?> <departments> <department> <department_id>10</department_id> <department_name>Administration</department_name> </department> </departments>
When you want only the value (10)Â to return, just type
/departments/department/department_id/text()
Now, also here various things are possible (I will just show a couple of the basic functionalities):
We can use boolean expressions to check:
//department[department_name="Administration"] <!-- returns true -->
or using the position() function, create a boolean:
/departments/department[position()=2] <!-- will return the second instance of department, nothing in the above case -->
or, using logical operators:
//department[@num>2 and @num<=4]/department_name <!-- will return department_names for departments with attribute "num" values 3 and 4 -->
There is also the possibility to use functions from within XPath expressions (seen above, as I already used the position() function. There is a whole bunch of XPath functions, too much to list all here.
XSL
With XSL, you can manipulate the way your XML document is represented. You can influence (using XPath expressions) what exactly from the XML document is shown in a html page, for example.
I will not go into detail now, because it is not completely clear to me yet (the basics, yes, but the somewhat more advanced features need to work their way inside my brain yet…). Anyway, you can use XSL style sheets to change the output of an XML document (in XML, HTML, plain text or various other formats), which makes it an essential tool for manipulating the data/data definitions for interfacing from application to application.
XML DB
Our final topic of the day was XML DB. We have gotten a rough introduction to XML DB, what it is and what it can do. Nothing more, nothing less. Hopefully tomorrow I will be back with more details on what we have been doing in the XML DB area.
For now, I will make an end to it. I hope you have enjoyed my writings sofar.
Arnoud,
Continue this bootcamp blogging!
Regards,
Bert
What sometimes is forgotten, and I became aware of it when I tried it, is that you can actually use the descriptions as defined in http://www.w3.org/TR/xpath. So for instance stuff like the following will also work: /departments/department/descendant-or-self::text(). See also here for examples http://www.zvon.org/xxl/XPathTutorial/General/examples.html. BE AWARE that some XPath functions work in XMLDB, but are not supported (like “position”)