Presentation is loading. Please wait.

Presentation is loading. Please wait.

Displaying XML Data with XSLT

Similar presentations


Presentation on theme: "Displaying XML Data with XSLT"— Presentation transcript:

1 Displaying XML Data with XSLT
Session 1 Introduction to XML Introduction to XSLT Programmatically Reading XML Documents Introduction to XPATH

2 What is XSL? XSL, or the eXtensible Stylesheet Language, is a language used for transforming XML data from one format to another. Realize that XSLT transforms an initial XML document to a different XML document! That is, after the transformation we are still left with an XML document, not text. For example, using XSL we can translate XML into XHTML, and thereby display XML data in a Web page. (XHTML is HTML formatted according to XML rules.)

3 What is XSL? XSL consists of three parts:
XSLT – (XSL Tranformation) the syntax used for transforming an XML document to another format. XPath – the syntax used to access particular elements or attributes of an XML document. XSL Formatting Objects – a syntax for formatting XML documents based on their data and for formatting data differently based on the device viewing the data. We will only be discussion XSLT and XPath. For more information on XSL Formatting Objects, see:

4 What is XSL? In this presentation, we’ll examine how to use XSLT to transform an XML document into HTML. However, XSLT can be used to transform XML format to any other text format. We’ll examine the .NET Framework’s XslTransform class, as well as the ASP.NET Xml Web control. Before we begin, though, we need to examine XPath.

5 XSLT – XSL Transform XSLT is a syntax for transforming XML data into an alternate format. The XSLT syntax can appear in an XML-formatted file (typically with a .xsl extension). The XSLT commands are designed to allow for iterating through a list of XML elements, and retrieving the text and/or attribute values of specific XML elements.

6 The Basic Structure of XSLT
The root element for an XSLT file is the <xsl:stylesheet> element. (Note the xsl namespace.) <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" … XSLT syntax goes here … </xsl:stylesheet>

7 A Quick Refresher on Namespaces
XSLT uses the xsl namespace, which is specified via: <xsl:stylesheet version="1.0" xmlns:xsl=" Note that the xsl namespace prefix is used. It’s namespace value must be the specific URL you see above…

8 The Components of XSLT Recall that XSLT is expressed as an XML-formatted document. There are a number of common XSLT elements that specify how the XML data should be transformed: <xsl:template> <xsl:for-each> <xsl:value-of>

9 Examining <xsl:template>
An XSLT document can contain an arbitrary number of <xsl:template> elements. Each <xsl:template> element must include a match attribute, whose value is an XPath expression that indicates what XML nodes the <xsl:template> tag will work with.

10 Examining <xsl:template>
For example, if we wanted to list each file on the filesystem, we’d need to first add an <xsl:template> element that matches with the <file> elements. <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/filesystem//file"> … </xsl:template> </xsl:stylesheet>

11 Example of <xsl:template>
<xsl:template match="/filesystem//file">

12 Examining <xsl:value-of>
The <xsl:value-of> element accesses the text or attribute value of an element. The <xsl:value-of> element requires a select attribute, which specifes the name of the element whose text node to retrieve, or the attribute name.

13 Example using <xsl:value-of>
The following XSLT document would output the names of all of the folders in the file system. <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/filesystem/drive//folder"> <xsl:value-of /> </xsl:template> </xsl:stylesheet>

14 Examining <xsl:for-each>
The <xsl:for-each> element iterates through all of the children elements of a specified element. The <xsl:for-each> element has a select attribute (like <xsl:value-of>), which specifies which element’s children to iterate through. <xsl:template match="/filesystem/drive"> <xsl:value-of /> <xsl:for-each select="folder"> <xsl:value-of /> </xsl:for-each> </xsl:template> What would this XSLT document’s output be?

15 Creating XSLT Files in VS.NET
Go to File/New/File and select the XSLT File option.

16 Creating XSLT Files in VS.NET
By default, VS.NET creates the following XSLT content for a new XSLT file: <?xml version="1.0" encoding="UTF-8" ?> <stylesheet version="1.0" xmlns=" </stylesheet>

17 Creating XSLT Files in VS.NET
However, to transform XML files with XSLT the XSLT file must contain the xsl namespace, like so (the added parts are in bold): <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" </xsl:stylesheet>

18 Fixing this Problem There are two ways to “fix” this problem:
Edit the XSLT file by hand each time you create a new one, adding the xsl namespace where needed. Edit the files: <VS.NET Dir>\Vb7\VBWizards\XSLTFile\Templates\1033\XSLTFile.xslt and - <VS.NET Dir>\VC#\CSharpProjectItems\XSLTFile.xslt

19 Converting XML to HTML Using XSLT
XSLT is commonly used to transform XML data to HTML. For example, we might want to display the file system information on a Web page in the following format: Drive Name All Folder Names in Drive File Name(s) for Specific Drive

20 Building the XSLT File Piecemeal
What XSLT syntax would we want to enumerate the <drive> elements? <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/filesystem/drive"> </xsl:template> </xsl:stylesheet> We could have done an <xsl:template match=“/”> and then an <xsl:for-each select=“filesystem/drive”>

21 Building the XSLT File Piecemeal
Now, how would we display the <drive> element’s letter attribute in a bold font? <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/filesystem/drive"> <b><xsl:value-of /></b> </xsl:template> </xsl:stylesheet>

22 Building the XSLT File Piecemeal
Now we need to iterate through all of the folders for the <drive> element using an unordered list. <xsl:stylesheet …> <xsl:template match="/filesystem/drive"> <b><xsl:value-of /></b> <ul> <xsl:for-each select=“//folder"> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet> Why //folder and not just folder?

23 Building the XSLT File Piecemeal
Now we need to show each <folder> element’s name attribute and then enumerate through its <file> elements. <xsl:template match="/filesystem/drive"> <b><xsl:value-of /></b> <ul> <xsl:for-each select=“//folder"> <li><xsl:value-of /></li> <ul> <xsl:for-each select=“file"> </xsl:for-each></ul> </xsl:for-each> </ul> </xsl:template>

24 Building the XSLT File Piecemeal
Finally, we need to display each <file> attribute’s text content (its filename). <xsl:for-each select=“//folder"> <li><xsl:value-of /></li> <ul> <xsl:for-each select=“file"> <li><xsl:value-of select=“text()" /></li> </xsl:for-each> </ul> </xsl:for-each>

25 Creating the XSLT File in VS.NET
Let’s create the XSLT file in VS.NET – please follow along. Now that we have an XSLT file, let’s see how to apply it to an XML document programmatically!

26 Using the XslTransform Class
The XslTransform class is found in the System.Xml.Xsl namespace. Like with the XmlDocument class, the first thing we need to do with an XslTransform class is Load XSLT into it via the Load() method. For more information on the XslTransform class, start with pg. 315 in the XML and ASP.NET book.

27 The XslTransform Class’s Load Method
The Load() method can accept: A string that points to a URL or file path, or An XmlReader class instance, or A class instance that implements the IXPathNavigable interface (typically an XmlNode, XmlDocument, or XmlPathDocument). Refer to pg. 318, Table 6.12

28 Transforming an XML Document
To transform an XML document with the XslTransform class, use the Transform() method. There are a number of variants of this method. For example, you can specify the input XML file and the output XML file, or the XmlDocument to transform and a Stream to write the output. Examine the docs…

29 Creating an Application to Perform Transformations
We will create a new VS.NET Windows Application (either in VB.NET or C#) (feel free to follow along). Our application will allow the user to, upon clicking a button, specify an XML file, an XSL file, and an output file, and the XML document will be transformed using the XSLT, with the resulting XML saved to the specified output file path.

30 Performing the Transformation
The source code for actually performing the transformation is trivial: XslTransform xslt = new XslTransform(); xslt.Load(xslFile); xslt.Transform(xmlFile, outputFile); The above assumes xslFile is a file path for the XSL file, xmlFile a file path for the XML document and outputFile a file path for the transformed output to be written.

31 Performing the Transformation (in VB.NET)
Dim xslt as XslTransform = New XslTransform() xslt.Load(xslFile) xslt.Transform(xmlFile, outputFile)

32 When Using XslTransform
Be sure to add the appropriate Import or using statements. Imports System.Xml Imports System.Xml.Xsl - and for C# - using System.Xml; using System.Xml.Xsl;

33 Some Notes About the Transformation
Examine the transformed content: <?xml version="1.0" encoding="utf-8"?><b>C</b><br /><ul><li>My Programs</li><ul></ul><li>Games</li><ul></ul><li>Quake</li><ul><li>quake.exe</li><li>README.txt</li><li>EULA.txt</li></ul><li>SavedGames</li><ul><li>game1.sav</li><li>game2.sav</li></ul><li>Windows</li><ul><li>win.exe</li></ul><li>System32</li><ul></ul></ul><b>D</b><br /><ul><li>Backup</li><ul><li> bak</li><li> bak</li></ul></ul>

34 Some Notes About the Transform
Notice that the transform content contains the XML pre-processing directive: All of the content is jammed together – white space was not preserved. (This doesn’t really matter if you’re translating XML to be displayed in an HTML Web page, since HTML is not whitespace-sensitive.) <?xml version="1.0" encoding="UTF-8" ?>

35 “Fixing” the Output To remove the pre-processing directive, we can use the XSLT <xsl:output method="html" /> element. To preserve whitespace, we can use <xsl:preserve-space elements=“elementList” /> Note that both of these elements must appear at the same nesting level of the <xsl:template> element.

36 The New, “Fixed” XSLT Document
<?xml version="1.0" encoding="utf-8" ?> <xsl:stylesheet version="1.0“ xmlns:xsl=" <xsl:output method="html" /> <xsl:preserve-space elements="*" /> <xsl:template match="/filesystem/drive"> <b><xsl:value-of /></b><br /> <ul> <xsl:for-each select=".//folder"> <li><xsl:value-of /></li> <xsl:for-each select="file"> <li><xsl:value-of select="text()" /></li> </xsl:for-each> </ul> </xsl:template> </xsl:stylesheet>

37 Displaying Formatted XML Content in a Web Page
Displaying XSLT-formatted XML content in a Web page can be done in one of two ways: Client-side – This technique sends the raw XML and XSLT content to the Web browser, which then formats the XML. (User must use a Web browser that supports this features (IE 5.0+, for example).) Server-side – The XML is transformed on the Web server, and the transformed content is then sent to the Web browser – works with any Web browser.

38 Server-Side Transformation with ASP.NET
With ASP.NET, performing server-isde transformations is a breeze with the XML Web control. With the XML Web control, simply specify the XML file path and the XSL file path, and it does everything for you!

39 Server-Side Transformation with ASP.NET
For example, the following ASP.NET Web page would display the HTML in the filesystem transformation we examined just a few slides ago: <html> <body> <asp:xml runat="server“ DocumentSource="FileSystem.xml" TransformSource="FormatFileSystemData.xsl" /> </body> </html>

40 The Visitor’s Web Browser:
The HTML Sent to the Browser: The Visitor’s Web Browser:

41 For More Information on the ASP.NET XML Web Control…
See pgs in XML and ASP.NET See See

42 Conclusion XSL is designed to translate XML content from one form to another. XSL is comprised of: XPath XSLT XSL Formatting Objects (XSLFO or XFO) XSLT transformations are specified via an XML-formatted document that includes templates, for-each, and value-of constructs.

43 Questions?


Download ppt "Displaying XML Data with XSLT"

Similar presentations


Ads by Google