Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]

Similar presentations

Presentation on theme: "XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]"— Presentation transcript:

1 XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]

2 XSL - Overview XSL is the eXtensible Stylesheet Language XSL is an XML-based language, like WML; but where WML is a content format, XSL is a way to to format content. The process of applying XSL to an XML document is called transforming the document. You transform it from one language--XML--to another: HTML, or maybe WML, or text or who- knows-what; even to another XML language. XSL is to XML as style sheets are to HTML; but XSL is actually much more powerful.

3 XSL - Overview Where’s the action?  XSL is a client-side technology. Unlike ASP or JSP, it does all its work on the client’s PC.  This means that speed of prsentation of your data depends on the client’s speed. Most web pages don’t work that way!  Since XSL transformations (“XSLT”) are run by the client, your client needs to be capable of XSL. Not all clients are XSL-capable! –Internet Explorer 5.* or less are not XSL-capable. –Internet Explorer 6.0 or higher are XSL-capable. –Netscape 6 is mostly XSL-capable. –Your mobile phone is almost certainly not XSL-capable. Microsoft’s XML parser is named MSXML. MSXML 3.0 is MS’s first XSL-capable parser. 3.0 first shipped with Windows XP.

4 XSL - Overview What’s it look like?  Remember XML? This is a class about XML.  Sample XML: Hello, world!  And how it looks in IE 6:

5 XSL - Overview We’re going to use XSL to give visual meaning to those two new tags, document and underlined. HTML defines a set of tags that specify formatting rules;,, and the like all specify visual formatting rules. XSL allows us to define our own tags; just as means underline in HTML, we can make an tag of our own that will underline the XML content. An XSL document defines a list of matchings from a tag and its content to a template expansion of HTML (or other) text.

6 XSL - Example One Here’s a sample of XSL: <xsl:stylesheet version="1.0” xmlns:xsl="">

7 XSL - Example One...and here’s the result of applying it to the XML:

8 XSL - Example One So how’d that work? After I created the.xsl file, I added one line to the.xml file: When Internet Explorer loaded the XML file, it found this XSL directive and loaded the XSL file to find the formatting transformation. Then it applied the XSL rules to the XML data, and transformed the XML into HTML.

9 XSL - Templates and Nodes Remember, XML documents are trees : The current node :  The XSL processor walks the XML document tree, replacing each node with its matching transformation. As it goes, it keeps track of the current node. In your XSL code you can reference the attributes, value, and children of the current node. Root node Child node

10 XSL - Templates and Nodes A closer look at the XSL code:  The standard XML header is followed by the stylesheet tag:...  The stylesheet lays out a series of template tags:......  Within each template, the XSL specifies the rules of replacement. For example, replaces all instances of the “underlined” tag with a bit of HTML formatting--the tags--wrapped around the content.

11 XSL - Templates and Nodes The XSL template tags  As each node of the source XML is processed, its tag is matched against the list of ’s in the XSL. The XSL processor searches for a tage whose match field matches the XML tag name. If it finds one, it replaces the XML tag with the content specified in the body of the tag.  match=“/” will match the root node of the document.  The tag is XSL for “recurse within this tag”. When the XSL processor encounters this, it calls itself on the content within the current node.

12 XSL - Example Two A slightly more complex example: John Smith MSc which would render as if it weren’t for the XSL directive...

13 XSL - Example Two The XSL code: <xsl:stylesheet version="1.0” xmlns:xsl=""> Degree sought :

14 XSL - Example Two And the resulting transformed document:

15 XSL - Node values and attributes The tag is used to extract values from fields in the XML data.  The value of the select attribute contains an XPath expression. XPath is a language--a part of XSL--that identifies nodes in the source XML so that you can refer to them in your XSL content. Some XPath examples:  select=“/document/frednode” would select a tag that was the immediate child of a tag in the root of the XML tree.  select=“/document//frednode” would select a tag that was a child, directly or indirectly, of a root tag.

16 XSL - Node values and attributes For example, if we add 28 July 1973 to the end of the content and Birth date :, to the XSL, we get Birth date : July28, 1973 at the end of the output.

17 XSL - Node values and attributes Perhaps more interesting, the tag has a select=“...” attribute too.  By specifying what to apply the template recursion to, we can actually change the structure of the document: Here instead of mindlessly displaying the content inside the tag in the order it’s entered, we’re displaying it in the order specified. We’re changing the actual tree structure of the XML document as we transform it.

18 XSL - Example Three You can also use the select syntax to embed the XSL transformation of one node inside that of another: Now the tag will be transformed to include a bulletted printout of the tag.  This works even though the tag isn’t a child of the tag in our original content!

19 XSL - Loops XSL supports looping over all the similiar child nodes of a tag with the element.  This is handy for laying out tables or lists. For example, you can open your tag and then fill in all the rows.  The select attribute of the for-each tag acts as a search. You can specify the nodes you want to enumerate over, and they’ll be processed in the order in which they appear. You specify the nodes with an XPath path.  Again, this can change the fundamental structure of the document.

20 XSL - Example Four For example, this code will create a table of all the tags: <table border="1" cellpadding="1" cellspacing="1"> <xsl:apply-templates select="name" /> <xsl:apply-templates select="degree" />

21 XSL - XPath paths XPath is a complex and powerful way to identify the nodes in an XML document. The path you give is relative to the current node; the path structure is rather like a directory path.  select=“*” : All the immediate children of the current node  select=“bob” : All the immediate children of the current node that are tags  select=“fred/bob” : All the tags which are immediate children of the tags immediately beneath the current node  select=“fred//bob” : All the tags which are descendants--at any depth--of the tags immediately beneath the current node  select=“.//bob” : All the tags anywhere beneath the current node  select=“../bob” : All the tags beneath the parent of the current node  select=“/bob” : All the tags immediately beneath the root node  select=“//bob” : All the tags anywhere beneath the root node So in Example four, “//student” identified all the nodes anywhere beneath the root of the document that were tags.

22 XSL - XPath paths (Example Five)  XPath recognizes the @ symbol as a shortcut to indicate that you’re referring to a node’s attributes instead of the tag name itself. Adding a textcolor=“...” attribute to the tag will now change the displayed color. (I use the tag to set the value of the tag’s color attribute.)

23 XSL - More tags to play with - insert a tag in the output stream  generates a named and parametrised tag: This is text produces the output This is text - inlines another XSL file  Replaces the tag with the contents of another XSL file at the specified URL.  Keep in mind that this may mean a new internet request, which could take time.

24 XSL - More tags to play with - conditionally select an element  The body of the tag is only added to the stream if the test condition in the test attribute is true.  Example:,...will match each instance of a tag under a tag and recurse on the title; but it will add a comma after every tag which is not the last in its list. See also: the tag, a more powerful form of conditional statement.  Used in conjunction with and.

25 Recap XSL:  Transforms documents from XML to another language  Can transform the document’s structure, as well as content XSL tags:  XPath:  *; node; node1/node2; node1//node2; //node;./node;../node  @AttributeName

26 Bibliography O’Reilly’s XML Pocket Reference Robert Eckstein and Michel Casabianca; (c) 2001 O’Reilly.

Download ppt "XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]"

Similar presentations

Ads by Google