XSLT
C
ourse
by
Pavan
∗ 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
C
ourse
by
Pavan
∗ 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
C
ourse
by
Pavan
∗ 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
C
ourse
by
Pavan
∗ 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
C
ourse
by
Pavan
∗ The <xsl:if> element is used to put a conditional test against the content
of the XML file
∗ Eg: <xsl:if test="price &gt; 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
C
ourse
by
Pavan
∗ 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
C
ourse
by
Pavan
XPath
C
ourse
by
Pavan
∗ 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
C
ourse
by
Pavan
∗ Nodes:
∗ Element
∗ Attribute Text
∗ Namespace
∗ Processing-instruction
∗ Comment
∗ Document nodes
∗ Relationship of Nodes:
∗ Parent
∗ Children
∗ Siblings
∗ Ancestors
∗ Descendants
XPath Terminology
C
ourse
by
Pavan
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
C
ourse
by
Pavan
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
C
ourse
by
Pavan
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.
C
ourse
by
Pavan
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
C
ourse
by
Pavan
XQuery
C
ourse
by
Pavan
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.
C
ourse
by
Pavan
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.
C
ourse
by
Pavan
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.
C
ourse
by
Pavan
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 :)
C
ourse
by
Pavan

Day2 xslt x_path_xquery

  • 1.
  • 2.
    ∗ XSL: EXtensibleStylesheet 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 C ourse by Pavan
  • 3.
    ∗ A commonway 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 C ourse by Pavan
  • 4.
    ∗ Write anXML ∗ 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 C ourse by Pavan
  • 5.
    ∗ 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 C ourse by Pavan
  • 6.
    ∗ The <xsl:if>element is used to put a conditional test against the content of the XML file ∗ Eg: <xsl:if test="price &gt; 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 C ourse by Pavan
  • 7.
    ∗ Templates ∗ Arerules to apply to the data ∗ Similar to Procedures / Functions ∗ Two ways to call the templates ∗ <xsl:call-template> ∗ <xsl:apply-templates> XSLT - Basics C ourse by Pavan
  • 8.
  • 9.
    ∗ XPath isa 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 C ourse by Pavan
  • 10.
    ∗ Nodes: ∗ Element ∗Attribute Text ∗ Namespace ∗ Processing-instruction ∗ Comment ∗ Document nodes ∗ Relationship of Nodes: ∗ Parent ∗ Children ∗ Siblings ∗ Ancestors ∗ Descendants XPath Terminology C ourse by Pavan
  • 11.
    Expression Description Nodename Selectall 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 C ourse by Pavan
  • 12.
    Expression Description * Matchesany 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 C ourse by Pavan
  • 13.
    AxisName Result ancestor Selectsall 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. C ourse by Pavan
  • 14.
    AxisName Result Round(number) Roundsthe 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 C ourse by Pavan
  • 15.
  • 16.
    XQuery ∗ XQuery isa 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. C ourse by Pavan
  • 17.
    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. C ourse by Pavan
  • 18.
    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. C ourse by Pavan
  • 19.
    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 :) C ourse by Pavan