1 / 11

XSL Transformations

XSL Transformations. Transforming XML document into other (XML) documents. What is XSL?. XSL = eXtensible Stylesheet Language Stylesheet language for XML documents XSLT = XSL Transformation Transformation of XML documents into other formats, like XML documents HTML document

prisca
Download Presentation

XSL Transformations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XSL Transformations Transforming XML document into other (XML) documents XSL Transformations

  2. What is XSL? • XSL = eXtensible Stylesheet Language • Stylesheet language for XML documents • XSLT = XSL Transformation • Transformation of XML documents into other formats, like • XML documents • HTML document • Microsoft Word documents, etc • XSL itself is an XML application • Defined by an XML Schema • XSL uses XPath to navigate XML documents • XSL is a W3C recommendation • 1999: XSL Transformations (XSLT), version 1.0 • 2007: XSL Transformations (XSLT), version 2.0 • Version 3.0, work in progress XSL Transformations

  3. XSL document, example <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/students"> <html> <head><title>Students</title></head> <body> <h1>Students</h1> <table border="1"> <tr> <th>Number</th> <th>Name</th> <th>Address</th> </tr> <xsl:for-each select="student"> <xsl:if test="@id &gt; 14000"> <tr> <td><xsl:value-of select="@id"/></td> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="address"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> XSL Transformations

  4. Link and XML document to a XSL document <?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="text/xsl" href="students2html.xslt"?> <students> <student id="14593"> <name>Anders</name> <address>Roskilde</address> </student> <student id="1111"> <name>Bill</name> <address>London</address> </student> </students> XSL Transformations

  5. XSL <xsl:template …> element • <xsl:template match=”XPath expression”> • Finds the first (if any) occurrence matching ”XPath expression” • Examples • <xsl:template match=”/”> • Matches the whole document • <xsl:template match=”/students”> • Matches the element student • The root element in my example XML document • No match → nothing is translated XSL Transformations

  6. XSL <xsl:value-of …> element • <xsl:value-of select=“XPath expression"/> • Finds the first occurrence (if any) matching the ”XPath expression”, and sends the value to the output stream. • Examples • <xsl:value-of select="name"/> • Matches the name element in the current element, and sends the value of the name element to the output stream • <xsl:value-of select="@id"/> • Matches the id attribute (note the @) in the current element, and sends the value of the id attribute to the output stream XSL Transformations

  7. XSL <xsl:for-each …> element • <xsl:for-each select=”XPath expression"> • Finds every occurrence of elements matching ”XPath expression” • Like a loop in ordinary programming languages • Example • <xsl:for-each select="student"> • Matches every student element (in turn) XSL Transformations

  8. XSL <xsl:if …> element • <xsl:if test=“Boolean expression"> • Match if “Boolean expression” evaluates to true • Example • <xsl:if test="@id &gt; 14000"> • True if the value of the id attribute > 14000 • &gt; means greater than XSL Transformations

  9. XSL support in Microsoft Visual Studio • Microsoft Visual Studio Ultimate has some interesting XSL features • Support for writing XSL documents • Applying XSLT to XML files • Showing the output on the screen • If you convert to HTML you can even see the HTML • Right-click -> View source XSL Transformations

  10. XSL transformation on the client sideMost modern browsers can do XSL transformations • Browser GETs the XML file • Browser recognizes the link to the XSL file • Browser GETs the XSL file • Browser applies the XSL file to the XML file, and shows the output XSL Transformations

  11. XSL transformation on the server sideAn ASP.NET code-behind/C#, example usingSystem.Xml.XPath; usingSystem.Xml.Xsl; protected void Page_Load(object sender, EventArgs e) { string MyXmlPath = Request.PhysicalApplicationPath + "\\students.xml"; string MyXsltPath = Request.PhysicalApplicationPath + "\\students2Html.xslt"; XPathDocument xmlDoc = new XPathDocument(MyXmlPath); XslCompiledTransform XSLTransform = new XslCompiledTransform(); XSLTransform.Load(MyXsltPath); XSLTransform.Transform(MyXmlPath, null, Response.Output); } • Source • http://weblogs.asp.net/dotnetstories/archive/2011/02/06/displaying-xml-data-using-xslt-transformations-in-an-asp-net-site.aspx XSL Transformations

More Related