CIS 132 XML, XPath, XQuery · 2013. 3. 20. · XPath. •XPath is used to navigate through elements...

Preview:

Citation preview

CIS 132 – XML, XPath, XQuery

• XML data including DTDs and XML Schema for validation, and an introduction to the query and transformation languages XPath, XQuery,

and XSLT.

What is XML

•Extensible Markup Language: Open standard language from the World Wide Web Consortium to describe data in a very structured way. •It has a similar look as HTML, but where HTML is for displaying data, XML is only for describing data in a structured way. •XML unlike HTML is highly expandable meaning you can create your tags and name them whatever you want. •While both HTML and XML are case sensitive most browsers do not enforce the case sensitivity on HTML, but with XML case sensitivity is enforced. •XML has become the base language for many other languages, including XHTML, SOAP, RSS, Atom. •It is also the default formatting in many applications including: MS Office, OpenOffice, LibreOffice, etc. •Both XML & HTML require matching tags, i.d. <p>Must have </p>. However again many browsers will let you cheat on HTML, but not XML.

Sample XML <carinfo> <cars> <car> <color>red</color> <doors>2</doors> <paidFor>true</paidFor> </car> <car> <color>blue</color> <doors>4</doors> <paidFor>true</paidFor> </car> <car> <color>white</color> <doors>2</doors> <paidFor>false</paidFor> </car> </cars> </carinfo>

What is DTD

• Document Type Definition.

• It is a markup definition for XML, HTML, and others. We will look at it only for XML.

• This is a simple example of a DTD. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

• DTD can be either in the document or external.

DTD Continued

• DTD’s can also be used to describe a data structure much like a database table structure complete with column names and data-types.

<!ATTLIST img

src CDATA #REQUIRED

id ID #IMPLIED

sort CDATA #FIXED "true"

print (yes | no) "yes" >

XML & DTD work together

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE people_list [ <!ELEMENT people_list (person)*> <!ELEMENT person (name, birthdate?, gender?, socialsecuritynumber?)> <!ELEMENT name (#PCDATA)> <!ELEMENT birthdate (#PCDATA)> <!ELEMENT gender (#PCDATA)> <!ELEMENT socialsecuritynumber (#PCDATA)> ]> <people_list> <person> <name>Fred Bloggs</name> <birthdate>2008-11-27</birthdate> <gender>Male</gender> </person> </people_list>

XQuery

• A fully functional programming language designed to query XML data.

• Per W3C "The mission of the XML Query project is to provide flexible query facilities to extract data from real and virtual documents on the World Wide Web, therefore finally providing the needed interaction between the Web world and the database world. Ultimately, collections of XML files will be accessed like databases".

XQuery Continued

• XQuery serves a SQL like purpose to extract and manipulate XML data. It also may query relation databases directly. It also has the ability to create new XML structures.

• It is a programming language that provides traditional programming concepts with XML and SQL like ability.

Sample XQuery.

<html><head/><body> { for $act in doc("hamlet.xml")//ACT let $speakers := distinct-values($act//SPEAKER) return <div> <h1>{ string($act/TITLE) }</h1> <ul> { for $speaker in $speakers return <li>{ $speaker }</li> } </ul> </div> } </body></html>

Uses for XQuery.

• Extracting information from a database for use in a web service.

• Generating summary reports on data stored in an XML database.

• Searching textual documents on the Web for relevant information and compiling the results.

• Selecting and transforming XML data to XHTML to be published on the Web.

• Pulling data from databases to be used for the application integration.

• Splitting up an XML document that represents multiple transactions into multiple XML documents.

XPath.

• XPath is used to navigate through elements and attributes in an XML document.

• XPath is a major element in W3C's XSLT standard - and XQuery and XPointer are both built on XPath expressions.

• XPath is currently on version 2.0, but 3.0 is in the works.

• It is a full programming language which XQuery is based on.

XSLT

• Extensible Style sheet Language Transformations.

• A language for translating other documents into XML, usually other XML documents.

• XSLT is currently on version 2, but many browsers do not support that version yet.

XSLT Continued

The XSLT template processor takes one or more XML sources, plus one or more XSLT stylesheet modules, and processes them with the XSLT template-processing engine (the processor) to produce an output document. The XSLT stylesheet contains a collection of template rules: instructions and other directives that guide the processor in the production of the output document.

Template Processor

A template processor (aka template engine) is an application designed to combine one or templates with a data model to produce one or more result documents.

A data model may be a relation database, XML, flat file, spreadsheet or any other type of preformatted data.

http://en.wikipedia.org/wiki/Template_processor

Template Processor

• All template processing systems consist of at least these primary elements:

• an associated data model;

• one or more source templates;

• a processor or template engine;

• generated output in the form of result documents.

http://en.wikipedia.org/wiki/Template_processor

Template Engine

Template Engines can be written in traditional languages, specifically designed languages, vendor provided software, or some combination of the above.

Typical languages include Java, Perl, PHP, .Net, Ruby, etc.

Some of these languages have template processors built in, including Java, Ruby, Perl, and .Net

XSLT Examples.

<?xml version="1.0" ?> -- Sample inbound XML Doc.

<persons>

<person username="JS1">

<name>John</name>

<family-name>Smith</family-name>

</person>

<person username="MI1">

<name>Morka</name>

<family-name>Ismincius</family-name>

</person>

</persons>

XSLT Examples Continued

XSLT Template: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/persons"> <root> <xsl:apply-templates select="person"/> </root> </xsl:template> <xsl:template match="person"> <name username="{@username}"> <xsl:value-of select="name" /> </name> </xsl:template> </xsl:stylesheet>

New XML Document

<?xml version="1.0" encoding="UTF-8"?>

<root>

<name username="JS1">John</name>

<name username="MI1">Morka</name>

</root>

XML to XHTML translation

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:template match="/persons"> <html> <head> <title>Testing XML Example</title> </head> <body> <h1>Persons</h1> <ul> <xsl:apply-templates select="person"> <xsl:sort select="family-name" /> </xsl:apply-templates> </ul> </body> </html> </xsl:template> <xsl:template match="person"> <li> <xsl:value-of select="family-name"/><xsl:text>, </xsl:text><xsl:value-of select="name"/> </li> </xsl:template> </xsl:stylesheet>

XHTML Output

<?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Testing XML Example</title> </head> <body> <h1>Persons</h1> <ul> <li>Ismincius, Morka</li> <li>Smith, John</li> </ul> </body> </html>

Recommended