XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN
Mar 26, 2015
XML-XSL Introduction
SHIJU RAJANSHIJU RAJAN
Outline Brief OverviewBrief Overview What is XML?What is XML?
Well Formed XMLWell Formed XML Tag Name Rules Tag Name Rules Comments & Processing InstructionsComments & Processing Instructions XML ValidationXML Validation XML Name SpaceXML Name Space Special Character EntitiesSpecial Character Entities XML ExampleXML Example
Displaying XML with CSSDisplaying XML with CSS
Outline Contd…
What is XSL? Transforming XML: XSLT
An Example
Nodes and Xpath XPath Essentials Templates
Calling Templates
Outline Contd… Push vs. Pull Processing Selecting Elements and Attributes Decision Structure: Choose Decision Structure: If Looping XSLT Example XML to XHTML on the Server Conclusion
What is XML?
XML stands XML stands for EXtensible Markup Language Markup Language XML was designed to XML was designed to describe describe data XML tags are not predefined. You must define
your own tags XML uses a Document Type Definition (DTD) or
an XML Schema to describe the data
Well-formed XML Tags must be suitably namedTags must be suitably named Single Root ElementSingle Root Element Follows general tagging rules:
All tags begin and end • But can be minimized if empty: <br/> instead of <br></br>
All tags are case sensitive All tags must be properly nested
• <sd> <firstname>William</firstname> <lastname>Russell</lastname> </sd>
All attribute values are quoted• <book sno=“1”> W.Shakespeare </ book>
Tag Name Rules
Must start with letter, colon or underscoreMust start with letter, colon or underscore Must only letters and numbersMust only letters and numbers characters characters -- _ . :_ . : The colon is reserved for W3C useThe colon is reserved for W3C use
Comments & Processing Instructions You can embed comments in your XML just like You can embed comments in your XML just like
in HTMLin HTML : : <!-- Whatever is here (whether text or markup) will
be ignored on processing -->
A processing instruction tells the XML parser information it needs to know to properly process an XML document
<?xml-stylesheet type="text/xsl" href="xslexample.xsl"?>
XML Validation
Uses only specific tags and rules as codified by one of A document type definition (DTD) A schema definition
Only the tags listed by the schema or DTD can be used
Software can take a DTD or schema and verify that a document adheres to the rules
XML Name Space
XML Namespaces provide a method to avoid element name conflicts.
This XML document carries information in a table:
<table><tr><td>Food</td></tr></table>
This XML document carries information about a table:
<table> <name>London</name><size>100 Miles</size></table>
If these two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition.
Solving Name Conflicts using a Prefix This XML document carries information in a table:This XML document carries information in a table: <h:table> <h:tr> <h:td>Food</h:td> </h:tr> </h:table>
This XML document carries information about a city:
<f:table><f:table> <f:name>London</f:name> <f:name>London</f:name> <f:size>100 Miles</f:size><f:size>100 Miles</f:size> </f:table></f:table>
Using Namespaces
This XML document carries information in a table:This XML document carries information in a table:
<h:table xmlns:h="http://www.w3.org/TR/html4/"><h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr><h:tr>
<h:td>Food</h:td><h:td>Food</h:td>
</h:tr></h:tr>
</h:table></h:table>
This XML document carries information about a city:This XML document carries information about a city:
<f:table xmlns:f="http://www.w3schools.com/city"><f:table xmlns:f="http://www.w3schools.com/city">
<f:name>London</f:name><f:name>London</f:name>
<f:width>100 Miles</f:width><f:width>100 Miles</f:width>
</f:table></f:table>
The XML Namespace (xmlns) The XML Namespace (xmlns) Attribute Attribute
The XML namespace attribute is placed in the start tag of an element and has the following syntax:
xmlns:namespace-prefix="namespaceURI"
The address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.
Special Character Entities
There are 5 characters that are reserved for special There are 5 characters that are reserved for special purposes; therefore, to use these characters when purposes; therefore, to use these characters when not part of XML tags, you must use an entity not part of XML tags, you must use an entity reference:reference: & (ampersand) becomes: && (ampersand) becomes: & < (less than) becomes: << (less than) becomes: < > (greater than) becomes: >> (greater than) becomes: > ‘ ‘ (apostrophe) becomes: '(apostrophe) becomes: ' “ “ (quote) becomes: "(quote) becomes: "
XML Example <?xml version="1.0"?><?xml version="1.0"?><bookstore><bookstore> <book sno=”1”><book sno=”1”> <author>W.Shakespeare</author><author>W.Shakespeare</author> <title>Hamlet</title><title>Hamlet</title> <published>1997</published><published>1997</published> <price>2.95</price> <price>2.95</price> </book> </book> <book sno=”2”><book sno=”2”> <author>W.Shakespeare</author><author>W.Shakespeare</author> <title>Macbeth</title><title>Macbeth</title> <published>1989</published><published>1989</published> <price>9.95</price> <price>9.95</price> </book> </book> </bookstore></bookstore>
Displaying XML with CSS
Must put a processing instruction at the top of your XML file (but below the XML declaration): <?xml-stylesheet type="text/css" href="style.css"?>
Example style.css book{
font-size:12;
}
What is XSL?
XSL stands for EXtensible Stylesheet Language.XSL stands for EXtensible Stylesheet Language. XSL describes how the XML document should be XSL describes how the XML document should be
displayed! This is the Preferred language for displayed! This is the Preferred language for processing XML.processing XML.
XSL consists of three parts:XSL consists of three parts: XSLTXSLT - - a language for transforming XML documents XPath XPath - - a language for navigating in XML documents XSL-FOXSL-FO - - a language for formatting XML documents
Transforming XML: XSLT XML Stylesheet Language — Transformations XML Stylesheet Language — Transformations
(XSLT)(XSLT) A markup language and programming syntax for A markup language and programming syntax for
processing XML processing XML Is most often used to:Is most often used to:
Transform XML to HTML for delivery to standard Transform XML to HTML for delivery to standard web clientsweb clients
Transform XML from one set of XML tags to Transform XML from one set of XML tags to anotheranother
Transform XML into another syntax/systemTransform XML into another syntax/system
Example: XML Document:XML Document:
<?xml version="1.0"?><?xml version="1.0"?>
<greeting>Hello, World!</greeting><greeting>Hello, World!</greeting>
Desired Output:Desired Output: <html><html>
<head><title>Greeting</title></head><head><title>Greeting</title></head> <body><p>Hello, World!</p></body><body><p>Hello, World!</p></body> </html></html>
XSLT Stylesheet:XSLT Stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <html><head><title>Greeting</title></head> <body><p><xsl:value-of select="greeting"/></p></body> </html> </xsl:template> </xsl:stylesheet>
XLST XSLT is based on the process of matching XSLT is based on the process of matching
templates to nodes of the XML treetemplates to nodes of the XML tree Working down from the top, XSLT tries to match Working down from the top, XSLT tries to match
segments of code to:segments of code to: The root element The root element Any child nodeAny child node And on down through the documentAnd on down through the document
We can specify different processing for each We can specify different processing for each element if you wish element if you wish
Nodes and XPath
An XML document is a collection of nodes that An XML document is a collection of nodes that can be identified, selected, and acted upon using can be identified, selected, and acted upon using an Xpath statementan Xpath statement
Examples of nodes: root, element, attribute, textExamples of nodes: root, element, attribute, text
XPath Essentials
//book = Select all <book> elements of the root node//book = Select all <book> elements of the root node //book[@sno=‘1’] = Select all <book> elements of the root //book[@sno=‘1’] = Select all <book> elements of the root
node that have a sno attribute with the value ‘1’node that have a sno attribute with the value ‘1’ //book/author = Select all <author> elements that have an //book/author = Select all <author> elements that have an
<book> element as a parent<book> element as a parent A period (.) denotes the current context node (e.g., ./author A period (.) denotes the current context node (e.g., ./author
selects any author tag that is a child of the current nodeselects any author tag that is a child of the current node Two periods (..) denote the parent node of the current Two periods (..) denote the parent node of the current
contextcontext
Templates
An XSLT stylesheet is a collection of templates that act against specified nodes in the XML source tree
For example, this template will be executed when a <para> element is encountered: <xsl:template match="para"> <p><xsl:value-of select="."/></p> </xsl:template>
Calling Templates A template can call other templates By default (tree processing):
<xsl:apply-templates/> [processes all children of the <xsl:apply-templates/> [processes all children of the current node]current node]
Explicitly: <xsl:apply-templates select=“/book/author”/> <xsl:apply-templates select=“/book/author”/>
[processes all < author > elements of the current node][processes all < author > elements of the current node] <xsl:call-template name=“title”/> <xsl:call-template name=“title”/>
[processes the named template, regardless of the source[processes the named template, regardless of the source tree]tree]
Push vs. Pull Processing In push processing, the source document controls
the order of processing (e.g., CSS is strictly pull processing); e.g.,
<xsl:apply-templates/><xsl:apply-templates/> Pull processing can address particular elements in
the source tree regardless of position in the source document; e.g.,
<xsl:apply-templates select =“//title”/>=“//title”/>
Selecting Elements and Attributes
To select the contents of a particular element, use this <xsl:select>statement::
<xsl:select value-of=“XPATH STATEMENT”/><xsl:select value-of=“XPATH STATEMENT”/>
<xsl:select value-of=“/book/title”/><xsl:select value-of=“/book/title”/> To select the contents of an attribute of a
particular element, use an XPath statement like:
<xsl:select value-of=“/book[@sno]”/>
Decision Structure: Choose
A way to process data differently based on specified criteria; if you don’t need “otherwise”, you can use <xsl:if>
<xsl:choose><xsl:choose>
<xsl:when test="<xsl:when test="SOME STATEMENT">">
CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUECODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE
</xsl:when></xsl:when>
<xsl:when test="SOME OTHER STATEMENT"><xsl:when test="SOME OTHER STATEMENT">
CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUECODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE
</xsl:when></xsl:when>
<xsl:otherwise><xsl:otherwise>
DEFAULT CODE HERE, IF THE ABOVE TWO TESTS FAIL
</xsl:otherwise></xsl:otherwise>
</xsl:choose></xsl:choose>
Decision Structure: If
A decision structure when you don’t need a default decision (otherwise use xsl:choose instead)
<xsl:if test="SOME STATEMENT"><xsl:if test="SOME STATEMENT"> CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE TRUE
</xsl:if></xsl:if> <xsl:if test="SOME OTHER STATEMENT"><xsl:if test="SOME OTHER STATEMENT">
CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE
</xsl:if></xsl:if>
Looping
XSLT looping selects a set of nodes using an Xpath expression, and performs the same operation on each; e.g.,
<xsl:for-each select=EXPRESSION>
CODE HERE
</xsl:for-each>
XML to XHTML on the Server Following is the ASP source code for Following is the ASP source code for
transforming XML file to XHTML on the server:transforming XML file to XHTML on the server: <%<%
'Load XML'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = falsexml.async = false
xml.load(Server.MapPath("bookstore.xml"))xml.load(Server.MapPath("bookstore.xml"))
'Load XSL'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = falsexsl.async = false
xsl.load(Server.MapPath("bookstore.xsl"))xsl.load(Server.MapPath("bookstore.xsl"))
'Transform file'Transform file
Response.Write(xml.transformNode(xsl))Response.Write(xml.transformNode(xsl))
%>%>
Conclusion
Begin transitioning to XML Begin transitioning to XML now:now: XHTML and CSS for web files, XML for static XHTML and CSS for web files, XML for static
documents with long-term worthdocuments with long-term worth
Do not rely on browser support of XML
Thank You Thank You
Happy Programming!!!!!!Happy Programming!!!!!!