170 likes | 317 Views
Overview of XSL. Outline. We will use Roger Costello’s tutorial The purpose of this presentation is To give a quick overview of XSL To describe what is available in Costello’s tutorial. Introduction and Motivation. Web Services.
E N D
Outline • We will use Roger Costello’s tutorial • The purpose of this presentation is • To give a quick overview of XSL • To describe what is available in Costello’s tutorial
Web Services • A Web service is written using servlets, JSP or a similar technology • The Web service interacts with databases using JDBC • The Web service interacts with clients using a Web server • The Web service may interact with other applications
The Traditional Approach • The Web service generates HTML pages in order to interact with clients • Changing the HTML pages necessitates a change in the Java code • Different clients (e.g., pc, palm-pilot, cell phone) may require different HTML pages • Different versions of the Java code should be maintained • HTML is not suitable for information exchange, e.g., interacting with automated agents (knowbots)
The XML Approach: Separating Content from Style • The Web service generates XML documents • Automated agents get XML documents in response to requests • Other clients get HTML pages that are generated by adding style to the XML documents • It is easy to implement different styles for different clients
Where Should the Business Rules be Implemented? • An example of a business rule • All products are now on sale at a discount of 10% • Business rules may change frequently • Where should they be implemented? • Database application • Servlet or JSP • XML transformation (by means of XSLT)
XML Transformations • CSS can add style to XML, but it cannot transform an XML document • How can you change the price of a product in an XML document describing that product? • How can you add (to the XML document) the information that the product is on sale? • XSLT can do just that
Even Just Adding Style May Require a Transformation • Consider an XML document that describes several products and their prices • How can you create a table with two columns – one for products and and one for prices? • Cannot be done in CSS, but can be done in XSL
Different Types of Transformations • Transform an XML document into • An HTML page • Another XML document • A text file • Roger Costello’s tutorial covers the three types (and more)
How to Transform? • Write an XSL style sheet • Use an XSL processor to transform the XML document according the XSL style sheet, or • Add a Stylesheet PI (Processing Instruction) to the XML document and display the XML document using a browser (recent versions only, e.g., IE 6 or Netscape 7)
Roger Costello’s Tutorial • It describes the XSL language • It gives many examples • There are actual files of the examples • Each example is in a separate folder and it includes • An XML document • An XSL style sheet • The result of the transformation as produced by an XSL processor
A Few Things About XSL • XSL is a high-level, functional language • The syntax is a bit peculiar and possibly confusing • An XSL style sheet is a valid XML document • Valid with respect to the XSL namespace • Therefore, commands in XSL are XSL elements
How to Use the Examples • Since XSL is a high-level language, XSL style sheets are relatively short and easy to understand • It is easy to modify an existing style sheet into another style sheet with a similar functionality • The many examples of Costello are very useful as a starting point for writing your own style sheets
The Main Elements of XSL • <xsl:value-of select=“xpath-expression“/> • This element is for extracting values from the given XML document • <xsl:for-each select=“xpath-expression“/> • This element is for looping • <xsl:if test=“xpath-expression/>, <xsl:if test=“xpath-expression=value“/>, etc. • This element is for conditional processing
Examples of Xpath Expressions • /university/department/course • This Xpath expression matches any path that starts at the root, which is a university element, passes through a department element and ends in a course element • ./department/course[@year=2002] • This Xpath expression matches any path that starts at the current element, continues to a child which is a department element and ends at a course element with a year attribute that is equal to 2002
How to Write a Style Sheet for Transforming XML to HTML • Write a template in XHTML • Add some necessary elements and PI (Processing Instructions) • Inside the XHTML template, use XSL elements in order to extract values from the given XML document