Home | | Service Oriented Architecture | The Missing Piece: The XSLT Processor

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

The Missing Piece: The XSLT Processor

XSLT processors are widely available. When you select an XSLT processor, you must ensure that it is fully compliant with the XSLT 1.0 specification. Table 9.1 contains a list of the most popular XSLT 1.0–compliant processors. : Implementing Client-Side XSLT Processing, Implementing Server-Side XSLT Processing, JSP: Server-Side XSLT Processing.

The Missing Piece: The XSLT Processor

 

So far, we’ve developed the XML document and the XSL style sheet. Now we need an XSLT processor to generate the output document, as shown in Figure 9.2.

 

XSLT processors are widely available. When you select an XSLT processor, you must ensure that it is fully compliant with the XSLT 1.0 specification. Table 9.1 contains a list of the most popular XSLT 1.0–compliant processors.

 

TABLE 9.1     XSLT 1.0 Processors

 

 

Many other XSLT processors are available at www.xslt.com. The main difference between the various processors is the programming language supported. For example, the parsers from Apache, Sun Microsystems, and James Clark provide a Java API. The Microsoft parser provides an API for Visual Basic and Visual C++. The Apache parser also provides a C++ API; however, development on the C++ version of Xalan has been suspended.

 

Two techniques are available for performing the XSLT processing: client-side processing and server-side processing.

 

Client-side XSLT processing commonly occurs in a Web browser. The Web browser includes an XSLT processor and retrieves the XML document and XSL style sheet, as shown in Figure 9.4.

 

The client-side technique offloads the XSLT processing to the client machine. This mini-mizes the workload on the Web server. However, the disadvantage is that the Web browser must provide XSLT support. At the time of this writing, Netscape Communi-cator 6 and Microsoft Internet Explorer 6 support the XSLT 1.0 specification.


Microsoft Internet Explorer 5.x has very limited support for XSLT 1.0. The previous ver-sion of the Netscape browser, 4.x, provides no support for XSLT.

 

The client-side technique is applicable when you’re deploying an application in a con-trolled environment. For example, in a corporate environment, the system administrators can install the latest version of the Web browser that conforms to the XSLT 1.0 specifica-tion. Implementation details for client-side XSLT processing are provided later in this chapter.

 

If you are deploying the application on an extranet or the Internet, you will probably have little control over the type/version of browser installed on the client machines. If this is the case, you should implement the server-side technique.

 

Server-side XSLT processing occurs on the Web server or application server. A server-side process such as an Active Server Page (ASP), JavaServer Page (JSP), or Java servlet will retrieve the XML document and XSL style sheet and pass them to an XSLT proces-sor. The output of the XSLT processor is sent to the client Web browser for presentation. The output is generally a markup language, such as HTML, that is understood by the client browser. The application interaction is illustrated in Figure 9.5.

 


 

An advantage of the server-side technique is browser independence. As shown in the pre-ceding figure, the output document is simply an HTML file. This technique supports the older browser versions and makes the application more robust and versatile. Implementation details are provided later in this chapter.

 

Also, by utilizing the server-side technique, the application can support a diverse collec-tion of clients. The application can detect the user-agent, such as a WAP-enabled mobile phone, and send back a document containing a Wireless Markup Language (WML) tag. The WAP-enabled phone can render the content using the built-in WML mini-browser.

 

Implementing Client-Side XSLT Processing

 

In this section, we’ll implement client-side XSLT processing. As mentioned before, you will need a browser that supports XSLT 1.0, such as Netscape Communicator 6 or Microsoft Internet Explorer 6.

 

For client-side processing, the XML document requires a special processing instruction to reference the XSL style sheet. The processing instruction is <?xml-stylesheet>, and it has two attributes: type and href. The type attribute specifies the content type of the document to be retrieved (in this case, text/xsl). The href attribute is a URL reference to the style sheet. The href attribute supports absolute and relative URL references.

 

The following code example uses a relative URL reference for the style sheet book_view.xsl:

 

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

 

Listing 9.4 shows the updated version of the book.xml document. This version contains the special processing instruction to reference an XSL style sheet.

LISTING 9.4    <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>

 

No changes are required to the XSL style sheet. You only have to make sure the XSL style sheet is accessible by the reference in the XML document. In this example, the style sheet is located in the same directory as the XML document. Listing 9.5 contains the style sheet.

 

LISTING 9.5    <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>

 

This example requires a browser that supports XSLT 1.0, such as Netscape Communicator 6 or Microsoft Internet Explorer 6.

 

Start the Web browser and open the file <install_dir>\ch9_xsl\browser_demo\ book.xml. The XML document, book.xml, references the style sheet, book_view.xsl. When the book.xml file is loaded in the browser, the style sheet is applied and the output is rendered in the browser, as shown in Figure 9.6.

 

 

Implementing Server-Side XSLT Processing

 

In this section, we’ll implement the server-side processing technique. A number of server-side technologies are available, including Common Gateway Interface (CGI), ColdFusion, Hypertext Processor (PHP), and so on. This chapter focuses on server-side processing with Microsoft’s Active Server Pages (ASP) and Sun Microsystems’ JavaServer Pages (JSP).

 

 

ASP: Server-Side XSLT Processing

 

In order to develop using ASP, you will need the IIS Web server and the latest version of the Microsoft XML parser. The required components are listed below.:

 

   Microsoft IIS Web Server 5.0. This version of IIS is included with Microsoft Windows 2000 Professional. You can also use IIS 4.0 or Personal Web Server (PWS); however, you will have to install the Windows NT Option Pack 4. Refer to Microsoft’s Web site for details on adding ASP support to IIS 4.0 and PWS.

 

   Microsoft XML Parser 3.0. If you have IE 6 installed on your server machine, then MS XML Parser 3.0 is included. The MS XML Parser 3.0 is also available as a separate download from http://msdn.microsoft.com.

 

The setup instructions at the beginning of the chapter showed you how to download and extract the source code. You have two options for publishing the source code on the IIS server:

 

 

   Copy the files in <install_dir>\ch9_xsl\public_html to c:\Inetpub\wwwroot.

 

   Set up a virtual directory that points to <install_dir>\ch9_xsl\public_html

 

An ASP file accesses the Microsoft XML parser as a server object. The following ASP code creates an input source for the XML document and XSL style sheet. Once the docu-ments are loaded, the XML document is transformed based on the rules in the style sheet. The output of the document is returned to the Web browser using the Response server object. Listing 9.6 contains the file book_test.asp.

 

LISTING 9.6    <install_dir>\ch9_xsl\public_html\book_test.asp

 


<%@ Language=VBScript %> <%

 

set xml = Server.CreateObject(“Microsoft.XMLDOM”) xml.load(Server.MapPath(“book.xml”))

 

set xsl = Server.CreateObject(“Microsoft.XMLDOM”) xsl.load(Server.MapPath(“book_view.xsl”))

 

Response.Write(xml.transformNode(xsl))

 

%>

 

 

You can test this example by starting the IIS server and then accessing the file book_test.asp in a Web browser. For this example you can use any Web browser. Remember that server-side processing is browser independent, so there is no requirement for XSL in the browser. Figure 9.7 illustrates this concept by accessing the ASP file using an older version of the Netscape Communicator browser, version 4.7.


 


JSP: Server-Side XSLT Processing

 

Sun Microsystems provides a server-side technology that is very similar to ASP. Of course, the server-side scripting is accomplished in Java. In order to perform the server-side processing with JSP, you will need to install the Java Software Development Kit (SDK) along with a compliant JSP server container. Here’s a list of required components:

 

   Sun Microsystems’ Software Development Kit (SDK) 1.3 (or higher). The SDK is available at Sun’s Web site, http://java.sun.com/j2se. Follow the installation instructions provided with the SDK.

 

   Apache Tomcat Server 4. Apache Tomcat 4 is the official reference implementation for JSP 1.2 and Java Servlets 2.3. If your application server already supports JSP 1.1 or higher, there is no requirement to install Tomcat. Apache Tomcat 4 is avail-able from the Apache Web site, http://jakarta.apache.org/tomcat. Follow the installation instructions provided with the Tomcat server.

 

Once Tomcat 4 is installed, you need to add a new Web application that points to the source code directory. This is accomplished by editing the file <tomcat_install_dir>\ conf\server.xml. Move to the section where the <Context> elements are listed and then add the following entry:

 

<Context        path=”/bookch9” docBase=”<install_dir>/ch9_xsl/public_html” debug=”0” reloadable=”true”  />

 

Be sure to update <install_dir> with the installation directory for the book’s source code. This configuration allows you to access the Web application named bookch9. This Web application’s document base is located at <install_dir>\ch9_xsl\public_html.

 

Restart the Tomcat server to pick up the new configuration. By default, the Tomcat server is listening on port 8080. You can access files for the bookch9 Web application using the URL http://localhost:8080/bookch9/book_test.jsp.

 

This example makes use of a JSP custom tag for the XSLT processing. A JSP custom tag is a special tag that is created by a developer. When the JSP server encounters the custom tag, it executes the handler code associated with the tag. JSP custom tags are conceptu-ally similar to ASP server objects. However, the custom action is represented in the JSP page as a custom tag instead of scripting code.

 

The Apache <jakarta:apply> tag provides the XSLT processing. The JSP code example shown in Listing 9.7 utilizes the <jakarta:apply> tag. This listing contains the file book_test.jsp.

 

LISTING 9.7    <install_dir>\ch9_xsl\public_html\book_test.jsp

 

<%@  taglib  uri=”http://jakarta.apache.org/taglibs/xsl-1.0”  prefix=”jakarta”  %>

 

<jakarta:apply  xml=”book.xml”  xsl=”book_view.xsl”  />

 

The first line in this example informs the JSP server to use the tag library that is identi-fied by the URI http://jakarta.apache.org/taglibs/xsl-1.0. This URI is defined in the Web application’s deployment description. The URI is actually mapped to the file jakarta-xsl.tld, located in the directory <install_dir>\ch9_xsl\public_html\WEB-INF. The file jakarta-xsl.tld is the Tag Library Descriptor (TLD). The TLD file pro-vides a description of the custom tags available in the class library. It also provides a mapping between the custom tag name and the tag handler class. The tag handler class is located in the directory <install_dir>\ch9_xsl\public_html\WEB-INF\lib.

 

The next line of code is the actual <jakarta:apply> tag. This tag has two attributes— one defines the XML input source and the other defines the XSL style sheet. The results of the XSLT process are returned to the Web browser.

 

To test this example, make sure the Tomcat server is running. In a Web browser, access the JSP with the URL http://localhost:8080/bookch9/book_test.jsp. The output should resemble Figure 9.7.


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


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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