6. Using CSS
The information can not be
rearranged(sorting etc.)
Information encoded in attributes can not
be exploited.
Additional structures can not be
introduced.
<?xml-stylesheet type="text/css"
href="ex_css.css"?>
9. XSLT
To transform an XML document into
another XML document, or another type of
document that is recognized by a browser,
like HTML and XHTML
Overcomes the limitations of the CSS
can add(remove) elements and attributes to (
from) the output file.
can also rearrange elements
can perform tests and make decisions about
which elements to hide and display
10. Declaration
Root element is “stylesheet”
Namespace:
http://www.w3.org/1999/XSL/Transform
To use this namespace, it is required to
include the attribute version="1.0".
Example:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
11. Linking XSLT to XML Doc.
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href=“abc.xsl"?>
<root_element>
….
….
….
</root_element>
12. Template Element
Template is set of rules
The rules defined by template are applied when the
specified node is matched
Syntax:
<xsl:template>
Attribute:
The match attribute is used to associate a template
with an XML element.
Value of match is an XPath expression.
Eg: <xsl:template match="/">
For root element
<xsl:template match=" catalog/cd /title">
To select title element
14. value-of Element
to extract the value of a selected node and
add it to the output stream of the
transformation
Eg:
<xsl:value-of select="catalog/cd/title"/>
select attribute:
Value is “XPath Expression”
text contents of the node-set returned by the
XPath expression are placed in the result
tree.
19. apply-template Element
applies a template to the current element
or to the current element's child nodes.
select attribute:
it will process only the child element that
matches the value of the attribute.
It can be used to specify the order in which
child nodes are processed.
22. sort Element
Sorts the output
Eg:
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist“
order=“ascending”/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
The result tree will have cd elements in the
ascending order of element artist.
23.
24. if Element
put a conditional test against the content
of the XML file
Syntax:
<xsl:if test=“test_expression">
...some output if the expression is true...
</xsl:if>
test attribute:
Specifies the expression to be evaluated.
32. Element “element” is used to create an
element.
Attribute “name” is used to specify the
name of the newly created element.
Value of attribute name is an XPath
expression.
33. Example
<?xml version = "1.0"?>
<sports>
<game title = "cricket">
<id>243</id>
<para> More popular among commonwealth nations. </para>
</game>
<game title = "baseball">
<id>431</id>
<para> More popular in America. </para>
</game>
<game title = "soccer">
<id>123</id>
<para>28 Most popular sport in the world. </para>
</game>
</sports>
34. stylesheet
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match = "sports">
<sports>
<xsl:apply-templates/>
</sports>
</xsl:template>
<xsl:template match = "game">
<xsl:element name = "{@title}">
<xsl:attribute name = "id">
<xsl:value-of select = "id"/>
</xsl:attribute>
<comment>
<xsl:value-of select = "para"/>
</comment>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
35. result.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<sports>
<cricket id = "243">
<comment> More popular among commonwealth nations. </comment>
</cricket>
<baseball id = "431">
<comment> More popular in America. </comment>
</baseball>
<soccer id = "123">
<comment> Most popular sport in the world. </comment>
</soccer>
</sports>
37. To duplicate nodes from the source tree
into the result tree
Element “copy” is used to produce a
copy of the context node and place it in
the result tree.
Syntax: <xsl:copy>
child nodes and attributes of the current node
are not automatically copied!
38. Intro.xml
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message>Welcome to XSLT!</message>
</myMessage>
39. Intro.xsl
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0“ xmlns:xsl = “http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "myMessage">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match = "message">
<xsl:copy>
How about 'Hi World' for a change!
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
40. Result.xml
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message> How about „Hi World‟ for a change! </message>
</myMessage>
41. copy-of Element
creates a copy of the current node.
Namespace nodes, child nodes, and
attributes of the current node are
automatically copied as well
This element can be used to insert
multiple copies of the same node into
different places in the output.
42. stylesheet
<?xml version = "1.0"?>
<xsl:stylesheet version = "1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "myMessage">
<xsl:copy-of select = "."/>
</xsl:template>
</xsl:stylesheet>
43. result.xml
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "intro.xsl"?>
<myMessage>
<message>Welcome to XSLT!</message>
</myMessage>
49. include Element
Include element can be used to refer to
another xslt doc.
The attribute “href” specifies the path of
the xsl doc to be included.
The local and the included both the
templates have the same precedence.
If any conflict arises, the template that last
occurs is used.
54. Variables
variable element is used to declare a local
or global variable.
global variable: declared as a top-level element
local variable: declared within a template
Once variable's value is set,it cannot be
changed or modifued
set value to a variable by the content of the
<xsl:variable> element or by the select
attribute