Displaying XML Data with XSLT Session 1 Introduction

  • Slides: 43
Download presentation
Displaying XML Data with XSLT Session 1 Introduction to XML Introduction to XSLT Programmatically

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

What is XSL? n n n XSL, or the e. Xtensible Stylesheet Language, is

What is XSL? n n n XSL, or the e. Xtensible 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. )

What is XSL? n XSL consists of three parts: 1. 2. 3. XSLT –

What is XSL? n XSL consists of three parts: 1. 2. 3. 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 formatting XML documents based on their data and 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: http: //www. w 3. org/TR/xsl/slice 6. html

What is XSL? n n n In this presentation, we’ll examine how to use

What is XSL? n n n 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 Xsl. Transform class, as well as the ASP. NET Xml Web control. Before we begin, though, we need to examine XPath.

XSLT – XSL Transform n n n XSLT is a syntax for transforming XML

XSLT – XSL Transform n n n XSLT is a syntax for transforming XML data into an alternate format. The XSLT syntax can appear in an XMLformatted 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.

The Basic Structure of XSLT n The root element for an XSLT file is

The Basic Structure of XSLT n 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="http: //www. w 3. org/1999/XSL/Transform"> … XSLT syntax goes here … </xsl: stylesheet>

A Quick Refresher on Namespaces n XSLT uses the xsl namespace, which is specified

A Quick Refresher on Namespaces n XSLT uses the xsl namespace, which is specified via: <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> • Note that the xsl namespace prefix is used. It’s namespace value must be the specific URL you see above…

The Components of XSLT n n Recall that XSLT is expressed as an XML

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

Examining <xsl: template> n n An XSLT document can contain an arbitrary number of

Examining <xsl: template> n n 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.

Examining <xsl: template> n For example, if we wanted to list each file on

Examining <xsl: template> n 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="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/filesystem//file"> … </xsl: template> </xsl: stylesheet>

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

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

Examining <xsl: value-of> n n The <xsl: value-of> element accesses the text or attribute

Examining <xsl: value-of> n n 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.

Example using <xsl: value-of> The following XSLT document would output the names of all

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="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/filesystem/drive//folder"> <xsl: value-of select="@name" /> </xsl: template> </xsl: stylesheet>

Examining <xsl: for-each> n The <xsl: for-each> element iterates through all of the children

Examining <xsl: for-each> n 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 select="@letter" /> <xsl: for-each select="folder"> What would this <xsl: value-of select="@name" /> XSLT </xsl: for-each> document’s </xsl: template> output be?

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

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

Creating XSLT Files in VS. NET n By default, VS. NET creates the following

Creating XSLT Files in VS. NET n 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="http: //www. w 3. org/1999/XSL/Transform"> </stylesheet>

Creating XSLT Files in VS. NET n However, to transform XML files with XSLT

Creating XSLT Files in VS. NET n 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="http: //www. w 3. org/1999/XSL/Transform"> </xsl: stylesheet>

Fixing this Problem n There are two ways to “fix” this problem: 1. 2.

Fixing this Problem n There are two ways to “fix” this problem: 1. 2. 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>Vb 7VBWizardsXSLTFileTemplates1033XSLTFile. xslt - and - <VS. NET Dir>VC#CSharp. Project. ItemsXSLTFile. xslt

Converting XML to HTML Using XSLT n XSLT is commonly used to transform XML

Converting XML to HTML Using XSLT n 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 n All Folder Names in Drive n File Name(s) for Specific Drive

Building the XSLT File Piecemeal n What XSLT syntax would we want to enumerate

Building the XSLT File Piecemeal n 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="http: //www. w 3. org/1999/XSL/Transform"> <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”>

Building the XSLT File Piecemeal n Now, how would we display the <drive> element’s

Building the XSLT File Piecemeal n 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="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/filesystem/drive"> <b><xsl: value-of select="@letter" /></b> </xsl: template> </xsl: stylesheet>

Building the XSLT File Piecemeal n Now we need to iterate through all of

Building the XSLT File Piecemeal n 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 select="@letter" /></b> <ul> <xsl: for-each select=“//folder"> </xsl: for-each> </ul> Why //folder and </xsl: template> </xsl: stylesheet> not just folder?

Building the XSLT File Piecemeal n Now we need to show each <folder> element’s

Building the XSLT File Piecemeal n 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 select="@letter" /></b> <ul> <xsl: for-each select=“//folder"> <li><xsl: value-of select="@name" /></li> <ul> <xsl: for-each select=“file"> </xsl: for-each></ul> </xsl: for-each> </ul> </xsl: template>

Building the XSLT File Piecemeal n Finally, we need to display each <file> attribute’s

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

Creating the XSLT File in VS. NET n n Let’s create the XSLT file

Creating the XSLT File in VS. NET n n 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!

Using the Xsl. Transform Class n n n The Xsl. Transform class is found

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

The Xsl. Transform Class’s Load Method n The Load() method can accept: n n

The Xsl. Transform Class’s Load Method n The Load() method can accept: n n n A string that points to a URL or file path, or An Xml. Reader class instance, or A class instance that implements the IXPath. Navigable interface (typically an Xml. Node, Xml. Document, or Xml. Path. Document). Refer to pg. 318, Table 6. 12

Transforming an XML Document n n To transform an XML document with the Xsl.

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

Creating an Application to Perform Transformations n n We will create a new VS.

Creating an Application to Perform Transformations n n 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.

Performing the Transformation n The source code for actually performing the transformation is trivial:

Performing the Transformation n The source code for actually performing the transformation is trivial: Xsl. Transform xslt = new Xsl. Transform(); xslt. Load(xsl. File); xslt. Transform(xml. File, output. File); The above assumes xsl. File is a file path for the XSL file, xml. File a file path for the XML document and output. File a file path for the transformed output to be written.

Performing the Transformation (in VB. NET) Dim xslt as Xsl. Transform = New Xsl.

Performing the Transformation (in VB. NET) Dim xslt as Xsl. Transform = New Xsl. Transform() xslt. Load(xsl. File) xslt. Transform(xml. File, output. File)

When Using Xsl. Transform n Be sure to add the appropriate Import or using

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

Some Notes About the Transformation n Examine the transformed content: <? xml version="1. 0"

Some Notes About the Transformation n 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>Quak e</li><ul><li>quake. exe</li><li>README. txt</li><li>EU LA. txt</li></ul><li>Saved. Games</li><ul><li>game 1. sav< /li><li>game 2. sav</li></ul><li>Windows</li><ul><li>wi n. exe</li></ul><li>System 32</li><ul></ul><b>D</b > <ul><li>Backup</li><ul><li>2003 -0601. bak</li><li>2003 -06 -07. bak</li></ul>

Some Notes About the Transform n Notice that the transform content contains the XML

Some Notes About the Transform n Notice that the transform content contains the XML pre-processing directive: <? xml version="1. 0" encoding="UTF-8" ? > n 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. )

“Fixing” the Output n n To remove the pre-processing directive, we can use the

“Fixing” the Output n n 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=“element. List” /> Note that both of these elements must appear at the same nesting level of the <xsl: template> element.

The New, “Fixed” XSLT Document <? xml version="1. 0" encoding="utf-8" ? > <xsl: stylesheet

The New, “Fixed” XSLT Document <? xml version="1. 0" encoding="utf-8" ? > <xsl: stylesheet version="1. 0“ xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> <xsl: output method="html" /> <xsl: preserve-space elements="*" /> <xsl: template match="/filesystem/drive"> <b><xsl: value-of select="@letter" /></b> <ul> <xsl: for-each select=". //folder"> <li><xsl: value-of select="@name" /></li> <ul> <xsl: for-each select="file"> <li><xsl: value-of select="text()" /></li> </xsl: for-each> </ul> </xsl: template> </xsl: stylesheet>

Displaying Formatted XML Content in a Web Page n Displaying XSLT-formatted XML content in

Displaying Formatted XML Content in a Web Page n Displaying XSLT-formatted XML content in a Web page can be done in one of two ways: n n 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.

Server-Side Transformation with ASP. NET n n With ASP. NET, performing server-isde transformations is

Server-Side Transformation with ASP. NET n n 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!

Server-Side Transformation with ASP. NET n For example, the following ASP. NET Web page

Server-Side Transformation with ASP. NET n 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“ Document. Source="File. System. xml" Transform. Source="Format. File. System. Data. xsl" /> </body> </html>

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

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

For More Information on the ASP. NET XML Web Control… n n n See

For More Information on the ASP. NET XML Web Control… n n n See pgs. 322 -324 in XML and ASP. NET See http: //aspfaqs. com/aspfaqs/Show. FAQ. asp? FAQID=204 See http: //msdn. microsoft. com/library/default. asp? url=/library/enus/vbcon/html/vbcon. XMLWeb. Server. Control. asp

Conclusion n n XSL is designed to translate XML content from one form to

Conclusion n n XSL is designed to translate XML content from one form to another. XSL is comprised of: n n 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.

Questions?

Questions?