OAUG Fusion Middleware BootCamp Chicago – Part III

2

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....

&lt;xsd:simpleType name=&quot;empid&quot;&gt;<br />  &lt;xsd:restriction base=&quot;xsd:positiveInteger&quot;&gt;<br />    &lt;xsd:maxInclusive value=&quot;1000&quot;/&gt;<br />  &lt;/xsd:restriction&gt;<br />&lt;/xsd:simpleType&gt;<br />&lt;xsd:element name=&quot;employee_id&quot; type=&quot;empid&quot;/&gt;

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&nbsp;

will return

&lt;department_id&gt;10&lt;/department_id&gt; 

from

&lt;?xml version=&quot;1.0&quot;?&gt;<br />&lt;departments&gt;<br />  &lt;department&gt;<br />    &lt;department_id&gt;10&lt;/department_id&gt;<br />    &lt;department_name&gt;Administration&lt;/department_name&gt;<br />  &lt;/department&gt;<br />&lt;/departments&gt;

When you want only the value (10) to return, just type

/departments/department/department_id/text()&nbsp;

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=&quot;Administration&quot;]  <br />&lt;!-- returns true --&gt;

or using the position() function, create a boolean:

/departments/department[position()=2] <br />&lt;!-- will return the second instance of department, nothing in the above case --&gt;

or, using logical operators:

//department[@num&gt;2 and @num&lt;=4]/department_name <br />&lt;!-- will return department_names for departments with attribute &quot;num&quot; values 3 and 4 --&gt;

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.

Share.

About Author

2 Comments