XML and XSL Transforming your XML documents with eXtensible Stylesheet Language Transformations [Optional Lecture]
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.
XSL - Overview Wheres the action? XSL is a client-side technology. Unlike ASP or JSP, it does all its work on the clients PC. This means that speed of prsentation of your data depends on the clients speed. Most web pages dont 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. Microsofts XML parser is named MSXML. MSXML 3.0 is MSs first XSL-capable parser. 3.0 first shipped with Windows XP.
XSL - Overview Whats it look like? Remember XML? This is a class about XML. Sample XML: Hello, world! And how it looks in IE 6:
XSL - Overview Were 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.
Slide 6 "> "> " title="XSL - Example One Heres a sample of XSL: ">
XSL - Example One Heres a sample of XSL:
XSL - Example One...and heres the result of applying it to the XML:
XSL - Example One So howd 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.
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
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.
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.
XSL - Example Two A slightly more complex example: John Smith MSc which would render as if it werent for the XSL directive...
Slide 13 Degree sought :"> Degree sought :"> Degree sought :" title="XSL - Example Two The XSL code: Degree sought :">
XSL - Example Two The XSL code: Degree sought :
XSL - Example Two And the resulting transformed document:
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.
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.
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 its entered, were displaying it in the order specified. Were changing the actual tree structure of the XML document as we transform it.
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 isnt a child of the tag in our original content!
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 theyll 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.