110 likes | 265 Views
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
E N D
XSL Transformations Transforming XML document into other (XML) documents XSL Transformations
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
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 > 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
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
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
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
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
XSL <xsl:if …> element • <xsl:if test=“Boolean expression"> • Match if “Boolean expression” evaluates to true • Example • <xsl:if test="@id > 14000"> • True if the value of the id attribute > 14000 • > means greater than XSL Transformations
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
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
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