Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

UNIVERSITY OF SOUK AHRAS

‫جــــامـعــة ســـوق أهــراس‬


FACULTÉ DES SCIENCES ET DE LA
‫كلية العلــــــــــــوم و تكنولوجيا‬
TECHNOLOGIE
‫قسم اإلعــــــالم اآللــــي‬
DEPARTEMENT OF COMPUTER SCIENCE

Semi-structured data course


3rd year License

Dr Ouanes AISSAOUI
Academic year: 2023-2024
Chapitre 3 : Technologies XML (40%)
1. XPath
2. XSLT
3. Applications XML : SVG,RDF
4. Traitement: DOM et SAX
5. Les pointeurs: XPOINTER
6. Les liens: XLINK
XSLT: Transforming XML Files

1. Principle
2. Basic rules
3. Construction of the result document
4. Iterative and conditional structures
5. Variables XSL
6. Parameterized rules, and with names
Introduction
• XSL = XML Stylesheet Language
• XSL is a style sheet language.
• XSL allows to define style sheets for XML
documents in the same way as CSS for HTML.
• XSLT-1.0 is a W3C Recommendation composed
of:
▫ XSLT: A Language of Transformation
▫ XPath: already studied, node selection
▫ XSL-FO: A set of XML formatting instructions for
presentation

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 4/38


Introduction (2)
• XSLT allows you to transform an XML document
into another XML document or into another
format, for the purposes of:
▫ Display: You can get an HTML document for
viewing (comparable to CSS). It is also possible to
produce text files, PDFs, etc.
▫ adaptation: we want to adapt the data contained
in the document to comply with a given DTD ...

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 5/38


XPath, XSL-FO
• XPath: XSLT allows you to specify a number of
operations to be performed when encountering
a particular element.
 XPath is used to designate the parts of the
document that we want to work on.
• XSL-FO: The formatting part includes:
▫ The formatting model: boxes, positioning,
ordering, how the pages are organized and how
the information "flows" from one page to another
▫ Display properties: text-align, margin-left,
background, fonts, ...
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 6/38
XSLT = XSL Transformations

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 7/38


XSLT Processor
• A style sheet evaluator reads an XSLT document
and an XML document and produces an output
document by applying the
instructions/specifications given in the style
sheet
• The evaluator can be integrated into a browser:
Firefox, Explorer, etc.
• or integrated into the server, for the dynamic
creation of web pages, e.g. the Apache XML
Cocoon project: http://cocoon.apache.org/

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 8/38


Structure of a style sheet
• A style sheet is an XML document of the shape

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 9/38


Execution Model
• An XSLT stylesheet consists of a prelude followed
by a sequence of rules for transforming the
shape:
<xsl:template match="path">
… transformations …
</xsl:template>
• A rule has two parts:
▫ a path pattern expressed in terms of the XPath
language (identifies a set of nodes)
▫ a transformation in the form of a sequence of
characters or elements, some of which are XSLT
statements
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 10/38
Execution Model (1)
• The evaluation of a template is done from a context
node. The initial context node is the root node
• From a context node, the evaluation takes place in the
following way:
▫ We are looking for the template with the most accurate
path model that matches the context node
▫ We instantiate the template chosen in the previous step.
The transformation can create a fragment of the resulting
document (by inserting text into the output stream) and
instantiate new templates. The evaluation then continues
recursively.
• A node set is evaluated by taking each of the nodes in
the set as the context node and then concatenating the
results.
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 11/38
Predefined rules
• Predefined rules apply in the absence of
applicable rules defined in the stylesheet. They
have a lower priority than these.

• Example: for the text and attribute nodes, their


values are copied into the output stream:
<xsl:template match= "text()|@*">
<xsl:value-of select="."/>
</xsl:template>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 12/38


Template tag
• This element defines a template to be applied to a
specific node and context.
• Usage:
<xsl:template name="myModel"
match="expressionXPath" mode="myMode">
</xsl:template>
• name : Assign a name to the template (optional)
• match : Indicates which nodeset will be affected by
the template (required)
• mode : Allows the same element to have multiple
templates, depending on the context (optional)
• priority : Used when there is a conflict between two
rules with the same condition (optional)
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 13/38
A first example
• Applying an XSL stylesheet to an XML document for
viewing in a web browser:
• 1/ Processing instruction in the XML file
<?xml-stylesheet type="text/xsl"
href="wg.xsl"?>
• 2/ Choosing a starting node in the style sheet
<xsl:template match="/">
• 3/ Basic Tag
<xsl:value-of select = "expressionXPath"/>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 14/38


A first example
<?xml version="1.0"?> wg.xsl
<xsl:stylesheet version="1.0"
<?xml version="1.0"?> gdt.xml
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<?xml-stylesheet
<xsl:template match="/"> type="text/xsl«
<html>
href="wg.xsl"?>
<head> <title>Présentation HTML de wg.xml</title> </head>
<gdt month="Février" year="2020">
<body>
<expose day="5">
<h1>Groupe de travail, <xsl:value-of select="gdt/@month"/>
<speaker>Abdelmalek
<xsl:value-of select="gdt/@year" Yahia</speaker>
/></h1>
<h2>Le <xsl:value-of select="gdt/expose/@day"/>
<title>Introduction audx BDD</title> <xsl:value-of
select="gdt/@month"/> :</h2>
<time>12h45</time>
<p>Présentation de <xsl:value-of select="gdt/expose/speaker" /></p>
<salle>102</salle>
<h3>Titre:</h3>
</expose> select="gdt/expose/title" /></p>
<p><xsl:value-of
</body>
</gdt>
</html>
</xsl:template>
</xsl:stylesheet>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 15/38


Construction of the result document
Several operations are possible:

• Insert the value of an input document node


• Copy a fragment of the input document
• Insert a new 'static' value element
• Insert a new 'dynamic' value element

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 16/38


Tag xsl:value-of
• This element is used to insert the value of a node into
the transformation.
• This node is selected by an XPath expression.
• This expression can be an element, attribute, or any
other node that contains a value:
<xsl:value-of select="XPathExpression" />
• select : The value of the Select attribute is evaluated,
and the result of this evaluation will be inserted during
the transformation. (required)
• For a node X of element type, the return value is the
concatenation of the text nodes of the X-rooted subtree
• For a set of nodes, only the value of the first node is
returned.
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 17/38
Copying a fragment
<xsl:copy-of select="path"/>
• Copies the fragment of the document to be
transformed selected by the XPath request from
the context node to the output stream.
• This fragment can be made up of several nodes.
• Example :
<xsl:copy-of select="gdt/expose"/>
Copies the designated "expose" elements and their
contents.

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 18/38


Fragment Result Element
<xsl:element name="tag">...
</xsl:element>

• Constructs a node with the name "tag" with the


attributes and contents defined in the body of the
xsl:element element

• Example :
<xsl:element name="p"><xsl:value-of select=“//param" />
</xsl:element>
• Creates a "p" tag containing the text present in the
"param" element of the original document.

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 19/38


Fragment Result Attribute
<xsl:attribute name="att">value</xsl:attribute>

• constructs an attribute named "att" (to be used inside an


xsl:element element)

• Example :
<xsl:element name="image">
<xsl:attribute name="src">
<xsl:value-of select=”@adresse”/>
</xsl:attribute>
....
</xsl:element>

• Creates an image tag, with an src attribute whose value is that of


the address attribute of the original element:
< image src ="test.gif"></image>
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 20/38
Short syntax
• In a stylesheet, you can actually insert new tags
directly:
▫ <image> ... </image>
• To refer to the original document, an element can
contain in its attributes "attribute expressions" of
the form {path}, where path is an XPath expression
that is evaluated to a string:
▫ <image src="{@adresse}"></image>
• Disadvantages of short syntax?
▫ hard to proofread if XPath expressions are long
▫ More complex automation

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 21/38


Short Syntax/Long Syntax?
• The short syntax can only be used if the name of
the element is "static". You can still have "dynamic"
attributes.

• Long syntax is mandatory to automate the


construction of "dynamic" elements.

• The short syntax is more concise, but it can be


more difficult to read if the Xpath expressions are
complex.

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 22/38


Example 2
<?xml version="1.0" ?>
<?xml version="1.0"?> Gid.xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/xsl">
<Guide> <xsl:template match="/">
<html><head><B>ESSAI XSL</B></head>
<Restaurant Categorie="**">
<Nom>Le Romantique</Nom>
<body><xsl:apply-templates/></body></html>
<Adresse>
</xsl:template>
<Ville>Cabourg</Ville>
<xsl:template match="Guide">
<Dept>Calvados</Dept>
</Adresse><H1>BONJOUR LE GROUPE XML</H1>
</Restaurant> <H2>SUIVEZ LE GUIDE</H2>
<xsl:apply-templates />
<Restaurant Categorie="***">
<Nom>Les</xsl:template>
TroisGros</Nom>
<Adresse> <xsl:template match="Restaurant">
<Ville>Roanne</Ville>
<P> <I>Restaurant :</I>
<Dept>Loire</Dept>
</Adresse>
<xsl:value-of select="Nom"/></P>
</Restaurant> </xsl:template>
Gid.xsl
</Guide> </xsl:stylesheet>
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 23/38
Iterative and conditional
structures

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 24/38


Tag xsl:apply-templates
• Within a rule, we need to use other rules.
• This tag is used to trigger the application of rules
on a set of nodes (the rules applied are chosen
according to the principle described above).
▫ <xsl:apply-templates/>
• The list of nodes to be processed consists of the
child nodes of the context node.
▫ <xsl:apply-templates select="path"/>
• The list of nodes to be processed consists of the
nodes reached by the path "path" from the context
node.

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 25/38


Tag xsl:for-each
<xsl:for-each select="XPathPath">
... instructions ...
</xsl:for-each>
• Applies the instructions to all nodes designated by the
XPath path.
• Example :
<xsl:template match=”Library”>
<ul>
<xsl:for-each select=”book”>
<li> Titre : <xsl:value-of
select="title"/>.</li>
</xsl:for-each>
</ul>
</xsl:template>
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 26/38
Conditional Expressions (1)
• You can test the (Boolean) value of an XPath expression and
run a template if the value is true :(in practice we often test
the existence of a node)
<xsl:if test="expression">… </xsl:if>
• Classic example:
<xsl:if test="book">
<ul>
<xsl:for-each select="book">
<li>
<xsl:value-of select="title" />.
<xsl:if test="@langue='English'">This book is in English</xsl:if>
</li>
</xsl:for-each>
</ul>
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 27/38
Conditional Expressions (2)
• The xsl:choose tag: Equivalent of the switch in C:
• Example:
<ul>
<xsl:for-each select="book">
<li>
<b><xsl:value-of select="author" /><br /></b>
<xsl:value-of select="title" />
<xsl:choose>
<xsl:when test="@langue='Frensh'">Ce livre est en
français.</xsl:when>
<xsl:when test="@langue='English'">This book is in
English.</xsl:when>
<xsl:otherwise>This book is in an unlisted language.</xsl:otherwise>
</xsl:choose>
</li>
</xsl:for-each>
</ul>
Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 28/38
Variables XSL

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 29/38


Variables
• A variable is a name associated with a value (these
are non-mutable variables).
• The value can be obtained by an XPath expression
or by the contents of the xsl:variable element.
• To get the value of a variable $name_var
• Examples :
<xsl:variable name='v1' select='12'/>
<xsl:variable name='v2' select='$v1+1'/>
<xsl:variable name='v3' select='@day'/>
<xsl:variable name='v4'>3</xsl:variable>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 30/38


Variables (2)
• A variable has a scope: the element in which it is
declared and its descendants.

• Examples : <date year=”2024” month=”Mars”


day=”$v3”/>

• A fragment of the input document can also be stored in


a variable

• Example: <xsl:variable name=”exposes”


select=”gdt/expose”/>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 31/38


Variables (2)
• A variable has a scope: the element in which it is
declared and its descendants.

• Examples : <date year=”2024” month=”Mars”


day=”$v3”/>

• A fragment of the input document can also be stored in


a variable

• Example: <xsl:variable name=”exposes”


select=”gdt/expose”/>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 32/38


Parameterized and named rules

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 33/38


Templates as Functions
• You can also give a rule a name and call it as a
function:

▫ <xsl:template name=« Temp1"> ... </xsl:template>


▫ <xsl:call-template name=« Temp1"/>

• It is a method that allows the same elements to be


used multiple times in a document.

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 34/38


Settings (1)
• A template can have parameters (again like a
function).

• At the beginning of the function, we declare the


parameters which uses:
<xsl:param name=’p1’/>

• To be used in combination with <xsl:with-param>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 35/38


Settings (2)
• An example: Defining the Function to a Parameter
<xsl:template name="polynome">
<xsl:param name="variable_x" />
<xsl:value-of
select="2*$variable_x*$variable_x+$variable_x+2" />
</xsl:template>

• Then call the function:


<xsl:call-template name="polynome">
<xsl:with-param name="variable_x" select="3.4" />
</xsl:call-template>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 36/38


Settings (3)
• A parameter is very similar to a variable. The only
difference is that its value can be changed.

• A parameter has a name, scope, and default value.


To get its $param value.

<xsl:param name="p1" select="expr"/>

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 37/38


Some instructions seen
• xsl:template : Defining a rule
• xsl:value-of, xsl:copy-of : insert (value of) a node
• xsl:element, xsl:attribute : Creating Nodes
• xsl:apply-templates, xsl:call-template : Rule
manipulations
• xsl:for-each : Loops
• xsl:if, xsl:choose : tests
• xsl:variable, xsl:param

Dr Ouanes AISSAOU Semi-structured data course, February 11, 2024 38/38


Thank you for your attention
Questions ?

o.aissaoui@univ-soukaharas.dz
ouanes.aissaoui@gmail.com

Ouanes AISSAOUI Semi-Structured Data 38/38

You might also like