Вы находитесь на странице: 1из 54

PART-1

INTRODUCTION TO XSLT
(Extensible Style Language for
Transformation)

12/7/21 Name of the presentation Page | 1


Background Of XSLT

 XSLT is part of a larger initiative within the World Wide


Web Consortium (W3C) to define a way of presenting
XML documents. This initiative is known as XSL
(Extensible Stylesheet Language).
 XSLT is an XML vocabulary that's used to define a
transformation between an XML document and a result
document, which might be in XML, in HTML, or a text
document.

12/7/21 Name of the presentation Page | 2


What is an XSLT Stylesheet?

 The XSLT processor uses a stylesheet to transform an


XML document.
 The stylesheet contains instructions for generating a new
document based on information in the source document.
 This can involve adding, removing, or rearranging nodes,
as well as presenting the nodes in a new way.

12/7/21 Name of the presentation Page | 3


The XSL Processing sequence

source
source document
document

source tree
XSL
XSL stylesheet
stylesheet XML
XML parser
parser

Result rules base


Result file
file
or
or stream
stream

apply
apply
write
write result
result templates
templates
to output
to output

result tree

12/7/21 Name of the presentation Page | 4


Transformation basics

 Transformation processor applies the rules on an XML document,


producing the output

 The output is a text document that may be an XML ,HTML,XHTML


document.
So, XSLT transformation allows us to transform XML in any text
based format.

12/7/21 Name of the presentation Page | 5


Simple example

12/7/21 Name of the presentation Page | 6


Dissected XSLT Stylesheet

12/7/21 Name of the presentation Page | 7


Result

rule

12/7/21 Name of the presentation Page | 8


Nodes and Node Trees

 When an application wants to operate on an XML


document it builds an internal model of what the
document looks like.
 This model is known as a document object model or
DOM.
 In XPath and XSLT, it's called a node tree.

12/7/21 Name of the presentation Page | 9


Types of Nodes
 Root nodes
ELEMENT ROOT
• The top of the node tree
 Element nodes document
document

• XML elements
ATTRIBUT para
para para
para para
para
 Attribute nodes
• XML attributes
E
note
note warning
warning warning
warning
 Text nodes
content
content content
content content
content
• Textual content in XML elements
 Comment nodes
• XML comments TEXT
 Processing instruction nodes
• XML processing instructions
 Namespace nodes
• The in-scope namespaces on an element

12/7/21 Name of the presentation Page | 10


Node Tree Example
<document>
<para type=“note”> content </para>
<para type=“warning”> content </para>
document
document
<para type=“warning”> content </para>
</document> para
para para
para para
para

note
note warning
warning warning
warning

content
content content
content content
content

- ro o t-
d ocum ent

- e le m e n t- - e le m e n t- - e le m e n t-
p a ra p a ra p a ra

- a ttr ib u te - - te x t- - a t tr ib u t e - - te x t- - a t tr ib u t e - - te x t-
ty p e : n o te c o n te n t t y p e : w a r n in g c o n te n t t y p e : w a r n in g c o n te n t

12/7/21 Name of the presentation Page | 11


XPATH

12/7/21 Name of the presentation Page | 12


Selecting nodes
 XSLT needs to be able to move through the document tree, to select
nodes or data, to describe the location of a node or a group of
nodes somewhere in a document
 These skills are provided by XPath
 XPath calls the description of the location a location path
 The structure of a location path:
• Axis – describes the direction to travel
• Node test – specifies what kinds of nodes are applicable
• Set of optional Boolean predicates – winnowing down the
candidates even further

12/7/21 Name of the presentation Page | 13


XPath Definition

 XPath is a language for addressing parts of an XML


document, designed to be used by XSLT.
 Example XPath: PREDICATE OR
FILTER
child::para[attribute::type='warning'][position()=2]

NODE TEST
AXIS
• select the second para child of the context node that has a type
attribute with a value of warning.

12/7/21 Name of the presentation Page | 14


Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]

Context node
d o cu m e nt

p a ra p a ra p a ra
Axis

ty p e :n o te ty p e :w a r n in g ty p e :w a r n in g

12/7/21 Name of the presentation Page | 15


Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]

Context node

d o cu m e nt
Filtered
p a ra p a ra p a ra
Axis

ty p e :n o te t y p e :w a r n in g ty p e :w a r n in g

12/7/21 Name of the presentation Page | 16


Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]

Context node

d o cu m e nt
Filtered
p a ra p a ra p a ra
Axis

ty p e :n o te t y p e :w a r n in g ty p e :w a r n in g

12/7/21 Name of the presentation Page | 17


Dissecting the Example
child::para[attribute::type='warning'][position()=2]

Axis: child::para
Filter: [attribute::type='warning']
Filter: [position()=2]

Context node

d o cu m e nt
Filtered
p a ra p a ra p a ra
Axis

ty p e :n o te t y p e :w a r n in g ty p e :w a r n in g Selected
12/7/21 Name of the presentation Page | 18
Types of XPaths

 Expressions:
Return a value, which might be a node set that is processed or a string
that is output.
<xsl:when test="@type=‘warning’">

 Patterns:
Either match a particular node or don't match that node.

<xsl:template match="para">

12/7/21 Name of the presentation Page | 19


XPath Expressions
 Select Nodes
<xsl:for-each select=“child::Z”> C

 Conditional Z Z

<xsl:if test=“position()=2”>
C

 Calculation 1 2 3 4
<xsl:value-of select=“position()+4">

12/7/21 Name of the presentation Page | 20


Node Set Expressions
 The most common way that XSLT uses XPaths is to select
node sets. These XPaths usually occur within a select
attribute, for example on xsl:for–each or xsl:apply–templates,
and are known as location paths.

<xsl:for-each select=“child::para”>

<xsl:apply-templates select="paragraph"/>

12/7/21 Name of the presentation Page | 21


Location Paths
 The purpose of location paths is to select node sets from a node
tree.
 Location paths can be absolute or relative R
• Absolute location paths
start from a known location
such as the root node N X Y N

<xsl:for-each select=“/R/N”> R

• Relative location paths


start from the context node. N X Y C ontext

<xsl:for-each select=“N”> N Z
note: same as “child::N”

12/7/21 Name of the presentation Page | 22


Some Shorthand Notation

 If no axis is specified in a step, the default axis is the child axis.


• Longhand select=“child::para/child::sentence”
• Shorthand select=“para/sentence”
 Another important axis is the attribute axis, which takes you from the
context node to the attributes of that node.
• Longhand select=“attribute::type”
• Shorthand select=“@type”
 Another essential axis is the parent axis. This takes you from the
context node to the parent of that node.
• Longhand select=“parent::node()/child::xyz”
• Shorthand select=“../xyz”

12/7/21 Name of the presentation Page | 23


Axes
 ancestor :: node()
Takes you up the tree to the ancestors of the context node in reverse
document order.
22

11

12/7/21 Name of the presentation Page | 24


Axes
 ancestor-or-self :: node()
Takes you up the tree to the ancestors of the context node in reverse
document order.
33

22

11

12/7/21 Name of the presentation Page | 25


Axes
 child :: node()
Takes you to the children of the context node in document order. This
is the default axis.

11 22

12/7/21 Name of the presentation Page | 26


Axes
 descendant :: node()
Takes you to the descendants of the context node. The resulting nodes
are in document order.

11 33

22 44 55 66

12/7/21 Name of the presentation Page | 27


Axes
 descendant-or-self :: node()
Takes you to the descendants of the context node, starting with the
context node, in document order.

11

22 44

33 55 66 77

12/7/21 Name of the presentation Page | 28


Axes
 following :: node()
Takes you to the nodes that occur after the context node in document
order, but that are not the context node’s descendants.

11 44

22 33

12/7/21 Name of the presentation Page | 29


Axes
 following-sibling :: node()
Takes you to the siblings of the context node that occur after the
context node in document order.

11 22

12/7/21 Name of the presentation Page | 30


Axes
 parent :: node()
Selects a single node – the parent of the context node.

11

12/7/21 Name of the presentation Page | 31


Axes
 preceding :: node()
Takes you to the nodes that occur before the context node in reverse
document order, excluding the context node’s ancestors.

33

22 11

12/7/21 Name of the presentation Page | 32


Axes
 preceding-sibling :: node()
Takes you to the siblings (children of the same parent) of the context
node that occur before it, in reverse document order.

22 11

12/7/21 Name of the presentation Page | 33


Axes
 self :: node()
Takes you to the context node itself.

11

12/7/21 Name of the presentation Page | 34


Example: Locating Nodes

 This XPath
expression selects all
the price elements of
all the cd elements of
the catalog element:
/catalog/cd/price
 This XPath
expression selects
all the cd elements
in the document:
//cd
*note most XPath expressions are written in shorthand

12/7/21 Name of the presentation Page | 35


Selecting Unknown Elements
 Wildcards ( * ) can be used to select unknown XML elements.
• This XPath expression selects all the child elements of all the cd
elements of the catalog element:
/catalog/cd/*

• The following XPath expression selects all the price elements that are
grandchild elements of the catalog element:
/catalog/*/price

12/7/21 Name of the presentation Page | 36


Selecting Unknown Elements

• The following XPath expression selects all price elements which have 2
ancestors:
/*/*/price

• The following XPath expression selects all elements in the document:


//*

12/7/21 Name of the presentation Page | 37


Selecting Branches

• The following XPath expression selects the first cd child element of


the catalog element:
/catalog/cd[1]

• The following XPath expression selects the last cd child element of the
catalog element (Note: There is no function named first()):
/catalog/cd[last()]

12/7/21 Name of the presentation Page | 38


Selecting Branches

• The following XPath expression selects all the cd elements of the


catalog element that have a price element:
/catalog/cd[price]

• The following XPath expression selects all the cd elements of the


catalog element that have a price element with a value of 10.90:
/catalog/cd[price=10.90]

12/7/21 Name of the presentation Page | 39


Selecting Branches
• The following XPath expression selects all the price elements of all the
cd elements of the catalog element that have a price element with a
value of 10.90:
/catalog/cd[price=10.90]/price

12/7/21 Name of the presentation Page | 40


Selecting Several Paths
• The following XPath expression selects all
the title and artist elements of the
cd element of the catalog element:
/catalog/cd/title | /catalog/cd/artist
• The following XPath expression selects all
the title and artist elements in
the document:
//title | //artist

• The following XPath expression selects all the title, artist and price
elements in the document:
//title | //artist | //price

12/7/21 Name of the presentation Page | 41


Selecting Attributes
• This XPath expression selects all
attributes named country:
//@country
• This XPath expression selects all
cd elements which have an attribute
named country:
//cd[@country]
• This XPath expression selects all
cd elements which have any attribute:
//cd[@*]
• This XPath expression selects all cd elements which have an
attribute named country with a value of 'UK':
//cd[@country='UK']

12/7/21 Name of the presentation Page | 42


XSLT INSTRUCTIONS

12/7/21 Name of the presentation Page | 43


Major XSL Instructions
 xsl:apply-imports  xsl:message
 xsl:apply-templates  xsl:namespace-alias
 xsl:attribute  xsl:number
 xsl:attribute-set  xsl:otherwise
 xsl:call-template  xsl:output
 xsl:choose  xsl:param
 xsl:comment  xsl:processing-instruction
 xsl:copy  xsl:sort
 xsl:copy-of  xsl:strip-space
 xsl:decimal-format  xsl:stylesheet
 xsl:element  xsl:template
 xsl:fallback  xsl:text
 xsl:for-each  xsl:transform
 xsl:if  xsl:value-of
 xsl:import  xsl:variable
 xsl:include  xsl:when
 xsl:key  xsl:with-param

12/7/21 Name of the presentation Page | 44


xsl:template
 The rule in the transformation is an XML element called
<xsl:template> whose contents are the elements and data that
will form the result subtree.
Here is an example of template rule:
<xsl:template match="/">
<html>
<head>
<title>My first template rule</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
</xsl:template>

12/7/21 Name of the presentation Page | 45


xsl:apply-templates
 The <xsl:apply-templates> element applies a template rule to the
current element or to the current element’s child nodes
 If we add a select attribute, it applies the template rule only to
the child that matches
 If we have multiple <xsl:apply-templates> elements with select
attributes, the child nodes are processed in the same order as the
<xsl:apply-templates> elements
Example:<xsl:template match="/">
  <html>
<body>
  <h2>My CD Collection</h2>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

12/7/21 Name of the presentation Page | 46


xsl:value-of
 <xsl:value-of select="XPath expression"/> selects the
contents of an element and adds it to the output stream
• The select attribute is required
• Notice that xsl:value-of is not a container, hence it needs to end with
a slash

 Example (from an earlier slide):


<h1> <xsl:value-of select="message"/> </h1>

12/7/21 Name of the presentation Page | 47


xsl:for-each
 xsl:for-each is a kind of loop statement
 The syntax is
<xsl:for-each select="XPath expression">
Text to insert and rules to apply
</xsl:for-each>
 Example: to select every book (//book) and make an unordered
list (<ul>) of their titles (title), use:
<ul>
<xsl:for-each select="//book">
<li> <xsl:value-of select="title"/> </li>
</xsl:for-each>
</ul>

12/7/21 Name of the presentation Page | 48


xsl:if
 xsl:if allows us to include content if a given condition (in the test
attribute) is true
 Example:
<xsl:for-each select="//book">
<xsl:if test="author='Terry Pratchett'">
<li>
<xsl:value-of select="title"/>
</li>
</xsl:if>
</xsl:for-each>

12/7/21 Name of the presentation Page | 49


xsl:choose
 The xsl:choose ... xsl:when ... xsl:otherwise construct is XML’s
equivalent of Java’s switch ... case ... default statement
 The syntax is:
<xsl:choose>
<xsl:when test="some condition">
... some code ...
</xsl:when>
<xsl:otherwise>
... some code ...
</xsl:otherwise>
</xsl:choose>

12/7/21 Name of the presentation Page | 50


xsl:sort
 You can place an xsl:sort inside an xsl:for-each
 The attribute of the sort tells what field to sort on
 Example:
<ul>
<xsl:for-each select="//book">
<xsl:sort select="author"/>
<li> <xsl:value-of select="title"/> by
<xsl:value-of select="author"> </li>
</xsl:for-each>
</ul>
• This example creates a list of titles and authors, sorted by author

12/7/21 Name of the presentation Page | 51


xsl:text
 <xsl:text>...</xsl:text> helps deal with two common
problems:
• XSL isn’t very careful with whitespace in the document
- This doesn’t matter much for HTML, which collapses all whitespace anyway
(though the HTML source may look ugly)
- <xsl:text> gives you much better control over whitespace; it acts like the
<pre> element in HTML
• Since XML defines only five entities, you cannot readily put other
entities (such as &nbsp;) in your XSL
- &amp;nbsp; almost works, but &nbsp; is visible on the page
- Here’s the secret formula for entities:

12/7/21 Name of the presentation Page | 52


Thank You

12/7/21 Name of the presentation Page | 53


Any Question?

12/7/21 Name of the presentation Page | 54

Вам также может понравиться