Download presentation
Presentation is loading. Please wait.
Published byEvelyn Padilla Modified over 11 years ago
2
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN
3
Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name Rules Tag Name Rules Comments & Processing Instructions Comments & Processing Instructions XML Validation XML Validation XML Name Space XML Name Space Special Character Entities Special Character Entities XML Example XML Example Displaying XML with CSS Displaying XML with CSS
4
Outline Contd… What is XSL? Transforming XML: XSLT An Example Nodes and Xpath XPath Essentials Templates Calling Templates
5
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
6
What is XML? XML stands Markup Language XML stands for EXtensible Markup Language XML was designed to describe XML was designed to 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
7
Well-formed XML Tags must be suitably named Tags must be suitably named Single Root Element Single Root Element Follows general tagging rules: All tags begin and end But can be minimized if empty: instead of All tags are case sensitive All tags must be properly nested William Russell All attribute values are quoted W.Shakespeare
8
Tag Name Rules Must start with letter, colon or underscore Must start with letter, colon or underscore Must only letters and numbers Must only letters and numbers characters - _. : characters - _. : The colon is reserved for W3C use The colon is reserved for W3C use
9
Comments & Processing Instructions You can embed comments in your XML just like in HTML : You can embed comments in your XML just like in HTML : A processing instruction tells the XML parser information it needs to know to properly process an XML document
10
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
11
XML Name Space XML Namespaces provide a method to avoid element name conflicts. This XML document carries information in a table: Food This XML document carries information about a table: London 100 Miles If these two XML documents were added together, there would be an element name conflict because both documents contain a element with different content and definition.
12
Solving Name Conflicts using a Prefix This XML document carries information in a table: This XML document carries information in a table: Food This XML document carries information about a city: London London 100 Miles 100 Miles
13
Using Namespaces This XML document carries information in a table: Food Food </h:table> This XML document carries information about a city: London London 100 Miles 100 Miles </f:table>
14
The XML Namespace (xmlns) 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.
15
Special Character Entities There are 5 characters that are reserved for special purposes; therefore, to use these characters when not part of XML tags, you must use an entity reference: There are 5 characters that are reserved for special purposes; therefore, to use these characters when not part of XML tags, you must use an entity 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: "
16
XML Example <bookstore> W.Shakespeare W.Shakespeare Hamlet Hamlet 1997 1997 2.95 2.95 W.Shakespeare W.Shakespeare Macbeth Macbeth 1989 1989 9.95 9.95 </bookstore>
17
Displaying XML with CSS Must put a processing instruction at the top of your XML file (but below the XML declaration): Example style.css book{ font-size:12; }
18
What is XSL? XSL stands for EXtensible Stylesheet Language. XSL stands for EXtensible Stylesheet Language. XSL describes how the XML document should be displayed! This is the Preferred language for processing XML. XSL describes how the XML document should be displayed! This is the Preferred language for processing XML. XSL consists of three parts: XSL consists of three parts: XSLT - XSLT - a language for transforming XML documents XPath - XPath - a language for navigating in XML documents XSL-FO - XSL-FO - a language for formatting XML documents
19
Transforming XML: XSLT XML Stylesheet Language Transformations (XSLT) XML Stylesheet Language Transformations (XSLT) A markup language and programming syntax for processing XML A markup language and programming syntax for processing XML Is most often used to: Is most often used to: Transform XML to HTML for delivery to standard web clients Transform XML to HTML for delivery to standard web clients Transform XML from one set of XML tags to another Transform XML from one set of XML tags to another Transform XML into another syntax/system Transform XML into another syntax/system
20
Example: XML Document: XML Document: Hello, World! Hello, World! Desired Output: Desired Output: Greeting Greeting Hello, World! Hello, World! XSLT Stylesheet: XSLT Stylesheet: Greeting
21
XLST XSLT is based on the process of matching templates to nodes of the XML tree XSLT is based on the process of matching templates to nodes of the XML tree Working down from the top, XSLT tries to match segments of code to: Working down from the top, XSLT tries to match segments of code to: The root element The root element Any child node Any child node And on down through the document And on down through the document We can specify different processing for each element if you wish We can specify different processing for each element if you wish
22
Nodes and XPath An XML document is a collection of nodes that can be identified, selected, and acted upon using an Xpath statement An XML document is a collection of nodes that can be identified, selected, and acted upon using an Xpath statement Examples of nodes: root, element, attribute, text Examples of nodes: root, element, attribute, text
23
XPath Essentials //book = Select all elements of the root node //book = Select all elements of the root node //book[@sno=1] = Select all elements of the root node that have a sno attribute with the value 1 //book[@sno=1] = Select all elements of the root node that have a sno attribute with the value 1 //book/author = Select all elements that have an element as a parent //book/author = Select all elements that have an element as a parent A period (.) denotes the current context node (e.g.,./author selects any author tag that is a child of the current node A period (.) denotes the current context node (e.g.,./author selects any author tag that is a child of the current node Two periods (..) denote the parent node of the current context Two periods (..) denote the parent node of the current context
24
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 element is encountered:
25
Calling Templates A template can call other templates By default (tree processing): [processes all children of the current node] [processes all children of the current node] Explicitly: [processes all elements of the current node] [processes all elements of the current node] [processes the named template, regardless of the source [processes the named template, regardless of the source tree] tree]
26
Push vs. Pull Processing In push processing, the source document controls the order of processing (e.g., CSS is strictly pull processing); e.g., Pull processing can address particular elements in the source tree regardless of position in the source document; e.g., =//title/>
27
Selecting Elements and Attributes : To select the contents of a particular element, use this statement: To select the contents of an attribute of a particular element, use an XPath statement like:
28
Decision Structure: Choose A way to process data differently based on specified criteria; if you dont need otherwise, you can use CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE DEFAULT CODE HERE, IF THE ABOVE TWO TESTS FAIL
29
Decision Structure: If A decision structure when you dont need a default decision (otherwise use xsl:choose instead) TRUE CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE CODE HERE TO BE EXECUTED IF THE STATEMENT IS TRUE
30
Looping XSLT looping selects a set of nodes using an Xpath expression, and performs the same operation on each; e.g., CODE HERE
34
XML to XHTML on the Server Following is the ASP source code for transforming XML file to XHTML on the server: Following is the ASP source code for 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 = false xml.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 = false xsl.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)) %> %>
35
Conclusion Begin transitioning to XML now: Begin transitioning to XML now: XHTML and CSS for web files, XML for static documents with long-term worth XHTML and CSS for web files, XML for static documents with long-term worth Do not rely on browser support of XML
36
Thank You Happy Programming!!!!!!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.