Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Study-Session: Part IV Transforming XML Documents Copyright Quddus Chong 2001.

Similar presentations


Presentation on theme: "XML Study-Session: Part IV Transforming XML Documents Copyright Quddus Chong 2001."— Presentation transcript:

1 XML Study-Session: Part IV Transforming XML Documents Copyright Quddus Chong 2001

2 Objectives By completing this study-session, you should be able to: n Learn to use the Xalan-Java XSL processor. n Understand basic XSL syntax and process model. n Perform your own XSLT operations. n Be able to put together XML solutions.

3 Why do we need XSL? n XML by itself is not capable of presentation. n A stylesheet is a declarative set of rules that defines how information elements identified in the source document should be rendered. n Two stylesheet standards are Cascading Style Sheets (CSS) and Extensible Stylesheet Language (XSL). n CSS cannot perform computations, rearrange or sort data, combine data from multiple sources, or personalize what is displayed to the user or session.

4 Two stages in XSL n Transformation (XSLT): Process of converting one XML document (or its in-memory representation) to another. n Formatting (XSL-FO): Process of converting the transformed tree structure into a two-dimensional graphical representation, or one- dimensional audio stream. In current practise, XSLT is used to transform XML into HTML, and a HTML browser is used as the formatting engine.

5 XSL Transformations n The W3C specification for XSLT ver. 1.0 can be found on http://www.w3.org/TR/xslthttp://www.w3.org/TR/xslt n An XSLT stylesheet is an XML document. n The basic processing paradigm is pattern-matching:  An XSLT stylesheet consists of a set of template rules, each of which takes the form “if this condition is encountered in the input, then generate the following output.” Template rules are applied recursively to each node in the XML document tree structure.

6 The XSLT Processing model XSLT Processor Fig. 1: The XSLT processor takes a tree structure as input, and generates another tree structure as output. ParserSerializer Result Document Stylesheet Source Document

7 Performing a simple transformation n From the XML Reference page, download and view the SimpleTransform.java sample code, the soccer.xml file, and the soccer.xsl file. n This program takes a user-specified source XSL file and a source XML file to create a user-specified output file. n To run, execute the compiled class file with the following arguments:  soccer.xsl soccer.xml soccer.html  prod-imp.xsl prod.xml prod1.html  prod-exp.xsl prod.xml prod2.html

8 Explanation of some steps in XSLT processing n xmlns keyword denotes a namespace, or qualifier for elements introduced from other XML vocabularies. n Whenever we intend to process the child nodes of an element, we are applying the templates that exist for those elements. Hence, this recursive action is called apply-templates. The XSL processor “walks” the source tree starting from the root node using a depth-first traversal and attempts to match each node to a corresponding template rule.

9 XML Path Language (XPath) n The W3C specification for XPath ver. 1.0 can be found at http://www.w3.org/TR/xpath.http://www.w3.org/TR/xpath n XSLT uses XPath to refer to nodes in the input tree. Xpath is essentially a addressing language matched to XML’s hierarchial data model (the document order). n Xpath expressions are used to select input nodes for processing, to test conditions during conditional processing, and to calculate values for insertion into the result tree.

10 Example XPath experessions n E.g. This select attribute value expresses all children of the current node named “answer”: select=“answer” n E.g. This match attribute value expresses a test of an element to see if it is of element type “question” or “answer”: match=“question|answer” n E.g. This example selects only the first “answer” child of the third “question” child of the current element: select=“question[3]/answer[1]” E.g. In this example, descendents of the element with identifier value “start” are examined, and those with elements with the answer attribute value of ‘y’ are selected: select=“id(‘start’)//question[@answer=‘y’]”

11 The Xalan-Java XSL processor n Open source XSL processor available as part of the xml.apache.org project. n Can be downloaded from as.zip file from http://xml.apache.org/xalan-j/.http://xml.apache.org/xalan-j/ n To use Xalan, you must include the xalan.jar and xerces.jar packages on your CLASSPATH. n To set classpath in Jcreator: Configure -> Options -> JDK Profiles -> select JDK version -> Edit -> Add Package -> add d:/xalan-j_2_0__1/bin/xerces.jar and d:/ xalan-j_2_0_1/bin/xalan.jar (in that order). n Please look at http://xml.apache.org/xalan-j/samples.html for more examples.http://xml.apache.org/xalan-j/samples.html

12 Processing XML with a JavaServer page (JSP) n If a webserver supports the running of server-side programs, then a browser visiting a page could trigger a webserver to run a program that reads a XML document and produces HTML. n A JavaServer page is a WWW document written in a mix of HTML and elements that are dynamically generated by executing Java code. n Custom tags that represent programming scripts to be dynamically executed can be included in a JSP page. n JSP can be used to convert XML to server-side objects, apply XSL transformations to a XML data source, or generate XML content for a client.

13 Using XML and JSP Fig. 2: Web application with heterogeneous clients XML JSP Page Parse and Transform via Custom Tag Applet HTML XML ClientWeb Layer Data Sources XSL (Applet) XSL (HTML) XSL (XML)

14 Examples and Case-Study n For examples of using XML with JSP, please look at:  “Developing XML Solutions with JavaServer Pages Technology” white paper (http://www.java.sun.com/products/jsp/html/JSPXML.html)http://www.java.sun.com/products/jsp/html/JSPXML.html  “Processing XML” website at University of Durham (http://www.dur.ac.uk/barry.cornelius/java/xml.processing/onefile/in dex.htm)http://www.dur.ac.uk/barry.cornelius/java/xml.processing/onefile/in dex.htm n For a case-study of DW-Travel, an e-travel website that uses JSP to serve XML data (via a graphical applet display), see the following article: (http://www.xmlmag.com/upload/free/features/xml/2000/04fal00/ ww0004/ww0004.asp)http://www.xmlmag.com/upload/free/features/xml/2000/04fal00/ ww0004/ww0004.asp

15 The Future of XML n XML as a universal data format. n More meaningful searches on the Web. n Upcoming applications of XML include:  SOAP (Simple Object Access Protocol)  VoiceXML  SVG (Scalable Vector Graphics)  SMIL (Synchronized Multimedia Integration Language)  WML (Wireless Markup Language)  XML Topic Maps  …the Next Killer Ap! (coming soon)


Download ppt "XML Study-Session: Part IV Transforming XML Documents Copyright Quddus Chong 2001."

Similar presentations


Ads by Google