This document summarizes key concepts about XML, XPath, XSLT, and XQuery. It provides examples of using XPath to select nodes from an XML document, using XSLT to transform XML documents to other formats like HTML, and using XQuery to query XML data. XPath is used to navigate XML, XSLT transforms XML documents, and XQuery combines XPath and FLWOR expressions to process, join, and return XML data. Examples demonstrate selecting nodes, transforming XML to XML and HTML, and the basic structure of XQuery with FLWOR expressions.
1. Tech Talk
BIBFRAME Working Group
17 November 2015
XPath, XSLT, and XQuery
Notes from the Library Juice Academy courses,“Introduction to XML” and
“Transforming and Querying XML with XSLT and XQuery”
Allison Jai O’Dell | AJODELL@ufl.edu || Hikaru Nakano | hnakano@mail.ufl.edu
Douglas Smith | dougsmith@uflib.ufl.edu || Gerald Langford | gerlang@uflib.ufl.edu
2. XML
“The Extensible Markup Language (XML) is a simple text-based format for
representing structured information: documents, data, configuration, books,
transactions, invoices, and much more. ” -- http://www.w3.org/standards/xml/core
3. XML Example
<?xml version="1.0" encoding="UTF-8"?>
<bunnies xmlns:food=“http://www.example.com/food”>
<bunny>
<name>Frances</name>
<breed>mini lop</breed>
<gender>female</gender>
<color>white with brown spots</color>
<birth>January 10, 2009</birth>
<food:fave>strawberries, parsley,
cilantro, carrots</food:fave>
</bunny>
<bunny status="RBB">
<name>Howard</name>
<breed>mixed, dwarf</breed>
<gender>male</gender>
<color>light brown agouti</color>
<birth>March 15, 2009</birth>
<death>September 1, 2012</death>
</bunny>
</bunnies>
• opening and closing tag
• case sensitive
• properly nested
• quoted attribute values
• opening XML declaration
• character encoding
• root element
• namespace declaration
5. XPath Examples
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Select all the title nodes:
/bookstore/book/title
Select all the year nodes, regardless of path:
//year
Select the title of the first book:
/bookstore/book[1]/title
Select price nodes with price>35:
/bookstore/book[price>35]/price
Select the attribute category "WEB" within book node; return titles
/bookstore/book[@category=“WEB”]/title
And you can use regular expressions!
http://www.w3schools.com/xsl/xpath_examples.asp
7. XSLT Overview
XSLT is used to transform an XML document into various types of documents, such as another
XML document, a web recognizable document (for example, HTML, XHTML, HTML5), and even
plain text documents.
How it works: The XSLT process utilizes XPath to navigate through the source tree and to identify
nodes in the source tree. The process then checks to see if the nodes that the XPath identifies
match a template that the user has defined. If a node matches a template, the transformation
defined in the template is performed.
8. Transformations using XSL
“A transformation in the XSLT language is expressed in the form of a stylesheet, whose syntax is
well-formed XML …
“The term stylesheet reflects the fact that one of the important roles of XSLT is to add styling
information to an XML source document, by transforming it into a document consisting of XSL
formatting objects (see [Extensible Stylesheet Language (XSL)]), or into another presentation-
oriented format such as HTML, XHTML, or SVG. However, XSLT is used for a wide range of
transformation tasks, not exclusively for formatting and presentation applications.”
-- http://www.w3.org/TR/xslt20/#what-is-xslt
9. How is XSLT used?
“If you make a purchase on eBay, or buy a book at Amazon, chances are that pretty much everything
you see on everyWeb page has been processed with XSLT. Use XSLT to process multiple XML
documents and to produce any combination of text, HTML and XML output. XSLT support is
shipped with all major computer operating systems today, as well as being built in to all majorWeb
browsers.”
-- http://www.w3.org/standards/xml/transformation
10. XSLT Example: XML to XML
Input:
ad.ufl.eduuflibdeptdataCatalogingAuthorities_&_Metadata_QualityBibFrameMeeting20151117pubmed_sample_xml_rev.docx
Output:
ad.ufl.eduuflibdeptdataCatalogingAuthorities_&_Metadata_QualityBibFrameMeeting20151117pubmed_sample_xslt_out.docx
16. XQuery Overview
There are a myriad of uses for XQuery including:
• querying XML documents and data sources that can output XML documents
• combining data from multiple sources
• transforming data
• generating reports from XML data
• building web and application services over XML data
There is some overlap in utility between XSLT and XQuery, but in general XQuery is more effective
in querying large structured and unstructured data sets and deriving data from large data sets.
17. FLWOR
XQuery works by combining the use of path expressions (XPaths) to access parts or fragments of
XML data and the use of FLWOR ("for", "let", "where", "order by", "return") expressions to process,
join, and return data.
http://www.w3ctutorial.com/xquery-basic/xquery-flwor