Home | | Service Oriented Architecture | Getting Started with XSLT

Chapter: XML and Web Services : Building XML-Based Applications : Transforming XML with XSL

Getting Started with XSLT

Creating the XML Document, Creating the XSL Style Sheet

Getting Started with XSLT

 

In the next example we’ll convert an XML document to an HTML document. The XML document contains a list of books, as shown is Listing 9.1.

 

LISTING 9.1    <install_dir>\ch9_xsl\browser_demo\book.xml

 

<?xml  version=”1.0”?>

 

<?xml-stylesheet  type=”text/xsl”  href=”book_view.xsl”?>

 

<book>

 

<author>Michael Daconta et al</author> <title>XML Development with Java 2</title> <category>Java</category>

 

<price currency=”USD”>44.99</price> <summary>

 

XML Development with Java 2 provides the information and techniques a Java developer will need to integrate XML into Java-based applications.

 

</summary>

 

</book>

 

The desired output of the HTML table is shown in Figure 9.3.


 

Creating the XML Document

 

The XML document, book.xml, contains elements for the author, title, price, summary, and category. Listing 9.2 has the complete code for book.xml.

 

LISTING 9.2    <install_dir>\ch9_xsl\browser_demo\book.xml

 

<?xml  version=”1.0”?>

 

<book>

 

<author>Michael Daconta et al</author> <title>XML Development with Java 2</title>

<category>Java</category>

 

<price currency=”USD”>44.99</price> <summary>

 

XML Development with Java 2 provides the information and techniques a Java developer will need to integrate XML into Java-based applications.

 

</summary>

 

</book>

 

In this example, we will apply the style sheet in a client-side Web browser. The XML document makes a reference to a style sheet using the following code:

 

<?xml-stylesheet  type=”text/xsl”  href=”book_view.xsl”?>

 

Creating the XSL Style Sheet

 

The next step is to create the XSL style sheet. XSL style sheets are XML documents; as a result, they must be well formed. An XSL style sheet has the following general struc-ture:

 

<?xml  version=”1.0”?>

 

<xsl:stylesheet  xmlns:xsl=”URI” version=”1.0”>

 

<!--XSL-T CONVERSION RULES-->

 

</xsl:stylesheet>

 

The <xsl:stylesheet> element defines how the XSLT processor should process the cur-rent XSL document. The xmlns attribute is the namespace definition. The XSL Transformation engine reads the xmlns attribute and determines whether it supports the given namespace. The xmlns attribute specifies the XSL prefix. All XSL elements and types in the document use the prefix.

 

The xmlns attribute value contains a Uniform Resource Identifier (URI), which serves as a generic method for identifying entities on the World Wide Web. It is important to note that the XSLT processor will not connect to the URI; it simply compares the URI against a collection of URIs that it supports.

 

The XSLT 1.0 specification defines the following URI for the XSL namespace:

 

http://www.w3.org/1999/XSL/Transform

 

The “1999” in the URI indicates the year the URI was allocated by the World Wide Web Consortium (W3C). It does not relate to the version of XSLT that is being used. The XSLT version reference is specified using the version attribute of the <xsl:stylesheet> element. The version attribute is required.

The XSL style sheet contains HTML text and XSL elements. The HTML text forms the basis of the desired output page. The XSL elements are template rules for the XSLT processor. A template is associated with a given element in the XML document. In our example, a template is defined to match on the <book> element using the following code:

 

<xsl:template  match=”/book”>

 

<!--static text and xsl rules -->

 

</xsl:template>

 

XSLT defines the <xsl:value-of> element for retrieving data from a XML document. The <xsl:value-of> element contains a select attribute. This attribute value is the name of the actual XML element you want to retrieve. For example, the following code will retrieve the title of the book:

 

<xsl:value-of  select=”title”  />

 

Now let’s create the file book_view.xsl. This style sheet will create an HTML page that contains information about the book, which is stored in the file book.xml. The style sheet contains the basic format of an HTML page and uses XSL elements to retrieve the data.

 

Currently, the XSL elements are merely placeholders in the document. Once an XSL processor accesses the XSL style sheet, the processor executes the XSL elements and replaces them with the appropriate data from the XML document. Listing 9.3 contains the complete code for book_view.xsl.

 

LISTING 9.3    <install_dir>\ch9_xsl\browser_demo\book_view.xsl

 

<?xml  version=”1.0”?>

 

<xsl:stylesheet  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”  version=”1.0”>

 

<xsl:template match=”/book”> <html><body>

 

<b>Title: </b> <xsl:value-of select=”title” /> <p/>

 

<b>By: </b> <xsl:value-of select=”author” /> <p/>

 

<b>Cost: </b> <xsl:value-of select=”price” /> <p/>

 

<b>Category: </b> <xsl:value-of select=”category” /> <p/>

 

<b>Description</b>

 

<p/>

 

<i><xsl:value-of  select=”summary”  /></i>

 

 

</body></html>

 

</xsl:template>

 

</xsl:stylesheet>

 

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
XML and Web Services : Building XML-Based Applications : Transforming XML with XSL : Getting Started with XSLT |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.