Upload
xavierconsultants
View
46
Download
0
Embed Size (px)
Citation preview
XSLT
Course
by P
avan
∗ XSL: EXtensible Stylesheet Language. ∗ Defined by the WWW Consortium (W3C) ∗ CSS – Stylesheets for HTML ∗ XSL – Stylesheets for XML ∗ XSL consists of three parts: ∗ XSLT - a language for transforming XML documents ∗ XPath - a language for navigating in XML documents ∗ XSL-FO - a language for formatting XML documents
Introduction
Course
by P
avan
∗ A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree
∗ XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML
∗ With XSLT you can add/remove elements and attributes to or from the output file.
∗ You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display
XSLT
Course
by P
avan
∗ Write an XML ∗ Write an XSLT ∗ Declaring XSLT: <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
∗ Link the XSLT to XML Document: <?xml-stylesheet type="text/xsl" href=“XSLName"?>
XSLT - Transformation
Course
by P
avan
∗ xsl:template Element ∗ Is used to build templates ∗ The match attribute is used to associate a template with an XML
element
∗ xsl:value-of element to select values from the XML elements. ∗ xsl:for-each element can be used to select every XML element of
a specified node-set ∗ Filtering the output:
∗ Filter the output from the XML file by adding a criterion to the select attribute
∗ Eg: <xsl:for-each select="catalog/cd[artist=‘SDTech']“/>
XSLT - Basics
Course
by P
avan
∗ The <xsl:if> element is used to put a conditional test against the content of the XML file ∗ Eg: <xsl:if test="price > 10"> … </xsl:if>
∗ The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. ∗ Eg: <xsl:choose>
<xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>
XSLT - Basics
Course
by P
avan
∗ Templates ∗ Are rules to apply to the data
∗ Similar to Procedures / Functions
∗ Two ways to call the templates
∗ <xsl:call-template> ∗ <xsl:apply-templates>
XSLT - Basics
Course
by P
avan
XPath
Course
by P
avan
∗ XPath is a W3C recommendation ∗ XPath contains a library of standard functions ∗ XPath uses path expressions to select nodes or node-sets in an
XML document. ∗ Helps you navigate through the XML document. ∗ Simple language consisting of path expressions. ∗ XPath uses expressions to select nodes or node-sets in an XML
document ∗ Each vendor has already defined a lot of XPath functions
XPath
Course
by P
avan
∗ Nodes: ∗ Element ∗ Attribute Text ∗ Namespace ∗ Processing-instruction ∗ Comment ∗ Document nodes
∗ Relationship of Nodes: ∗ Parent ∗ Children ∗ Siblings ∗ Ancestors ∗ Descendants
XPath Terminology
Course
by P
avan
Expression Description
Nodename Select all nodes with the name “nodename”
/ Selects from root nodes
// Selects nodes in the document from the current node that match the selection no matter where they are
. Selects the current node
.. Selects the parent of current node
@ Selects attributes
XPath Expressions
Course
by P
avan
Expression Description
* Matches any element node
@* Matches any attribute node
| Selects two nodes with AND
. Selects the current node
.. Selects the parent of current node
@ Selects attributes
XPath Wildcards
Course
by P
avan
AxisName Result
ancestor Selects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attribute Selects all attributes of the current node
child Selects all children of the current node
descendant Selects all descendants (children, grandchildren, etc.) of the current node
descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
following Selects everything in the document after the closing tag of the current node
following-sibling Selects all siblings after the current node
namespace Selects all namespace nodes of the current node
parent Selects the parent of the current node
preceding Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
preceding-sibling Selects all siblings before the current node
self Selects the current node
XPath Axes An axis defines a node-set relative to the current node.
Course
by P
avan
AxisName Result
Round(number) Rounds the number argument to nearest integer
Substring(string, start, len)
Returns sub-string from start position to specified length
String-length Returns length of the string
Day-from-datetime Returns the day of the date.
Max() Returns maximum value from set of numbers
XPath Functions
Lots of pre-defined functions. Few of them are below
Course
by P
avan
XQuery
Course
by P
avan
XQuery
∗ XQuery is a W3C recommendation ∗ XQuery : XML what SQL : database tables ∗ XQuery is designed to query XML data. ∗ XQuery is built on XPath expressions ∗ XQuery is supported by all major databases.
Course
by P
avan
XQuery – Basics
∗ Uses functions to extract data from XML documents. ∗ Uses path expressions to navigate through elements in an XML
document. ∗ Uses predicates to limit the extracted data from XML
documents. ∗ Can also transform the XML document into other format. ∗ XPath terminology and relationship applies to XQuery also.
Course
by P
avan
XQuery – Basics
∗ FLWOR is an acronym for "For, Let, Where, Order by, Return". ∗ The for clause selects all book elements under a node
element into a variable. ∗ The where clause selects only elements with a filter. ∗ The order by clause defines the sort-order. Will be sort by the
specified element. ∗ The return clause specifies what should be returned.
Course
by P
avan
XQuery – Syntax
∗ XQuery is case-sensitive ∗ XQuery elements, attributes, and variables must be valid XML
names ∗ An XQuery string value can be in single or double quotes ∗ An XQuery variable is defined with a $ followed by a name ∗ XQuery comments are delimited by (: and :), e.g. (: XQuery
Comment :)
Course
by P
avan