RSS RSS feed
November 21, 2009
Hot Topics:

Introduction to Transformations (XSLT) using IE5

  • April 6, 2000
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »


Preface

I have authored numerous online articles on XML.  These articles cover the waterfront from introductory topics to advanced topics.

I maintain a consolidated index of hyperlinks to all of my XML articles at my personal website so that you can access earlier articles from there.

Rendering XML documents

As of this writing, Microsoft IE5 is the only widely-used web browser that has the ability to render XML documents. IE5 can render XML documents using either CSS or XSL.

This is one in a series of articles that discuss the use of XSL for the rendering of XML documents, with particular emphasis on the use of IE5 for that purpose.

Introduction

In a previous article, I introduced you to
  • The W3C
  • Extensible Stylesheet Language (XSL)
  • XSL Transformations (XSLT).
Testing XSL and XSLT code

I also introduced you to four ways that you can test your XSL or XSLT code:

  • Sparse
  • IE5
  • The XSL Debugger
  • XT
Will walk through an example

In this lesson and the next, I am going to walk you through some XML code, and some corresponding XSLT code.  I am going to show you how to use IE5 to transform the XML according to the XSLT.

This example will be based on XSLT information available at the W3C and on IE5 information available at Microsoft.

Requires IE5 or later

These programs require IE5 or later.  As of the date of this writing, if you try to repeat these  results using Netscape 4.7, it won't work.

Hopefully Netscape will rectify this situation in the near future and provide XML rendering capability for their browser product as well.

Where does the transformation take place?

In the previous lesson, I told you that a transformation from XML to HTML can take place anywhere between the XML document and the browser.  I gave some examples of how you might use Java servlets to perform the transformation on the server.

Doing the transformation in the browser

In this lesson, there won't be a server involved.  Rather, the transformation will take place within the IE5 browser.

A Sample Program

For this sample program, I will show you how to create an XML file and a corresponding XSLT file.

When you load the XML file into IE5, the XML will be converted to HTML and displayed in the browser window.

The XML File

For this introduction, I purposely made the XML file very simple.  Despite that, there are a couple of things about it that will deserve some discussion, so I will discuss it in fragments.

Complete listing available later

A complete listing of the XML file is provided near the end of the lesson.

File XSL002.xml

For the record, this file is named XSL002.xml.  (Although you probably don't care, I am a college professor, and I need that information when I do demonstrations in the classroom.)

A stylesheet processing instruction

The XML file begins with what is commonly referred to as a stylesheet processing instruction (PI) as shown in Figure 1.

<?xml-stylesheet  
  type="text/xsl" 
  href="XSL002.xsl"?>

What are the parts of the PI?

According to Goldfarb, author of The XML Handbook,
 

"The xml:stylesheet processing instruction is allowed anywhere in the prolog of an XML document.  The processing instruction can have pseudo-attributes href (required), type (required), title (optional), media (optional), charset (optional)."

What is a pseudo-attribute?

According to Goldfarb,
 

"These are called 'pseudo-attributes' instead of attributes because, although they use attribute syntax, they do not describe properties of an element.  The only real syntactic difference between pseudo-attributes and attributes is that you must use pseudo-attributes in the order they are described.  You can use attributes in any order."

I provided the required attributes only

As you can see in Figure 1, I provided only those pseudo-attributes that are required.

Type

The type attribute specifies that the stylesheet is written in XSL, and not in CSS or some other stylesheet language.  (To see the use of stylesheets with IE5 that are written in CSS, take a look at examples that are available on my personal website.)

href

The href attribute is a URI that specifies where the stylesheet file can be obtained.  According to Microsoft, the following restriction applies to the use of IE5 and XSL style sheets:
 

The XSL style sheet must come from the same URL scheme (example: http) and host name (example: www.microsoft.com) as the XML source. The XSL style sheet download follows the same security policies as those for fetching an external entity. Attempting to read a style sheet from a different domain results in an access violation error unless the security option "Access data across domains" is selected from the Internet Options dialog box. 

I used a local stylesheet file

For this example, the stylesheet is available locally as a file named XSL002.xsl, in the same directory as the XML file, so it satisfies the restriction.

The top-level element and namespaces

Figure 2 shows the definition of the top-level element in the XML file named sampXML.

<sampXML>

The repeat elements

Figure 3 shows two elements in the XML file named repeat.

<repeat>
  <para>This is a paragraph.</para>
</repeat>

<repeat>
  <para>This is another paragraph.</para>
</repeat>

Plain vanilla XML

From an XML viewpoint, there is nothing special about these two elements.  Each of the repeat elements contains a nested element named para, and each of the para elements contains some text.

Nothing special about the element names

There is also nothing special about the names of these elements.  I could have called them anything (provided that the stylesheet references them appropriately.)

repeat elements will be in a loop

I elected to name the outer elements repeat because I am going to include them in a repeating loop in the stylesheet.

para elements will be turned into HTML paragraphs

I elected to name the inner elements para because I am going to cause the XSLT code to transform them into HTML paragraphs, but this is just a naming convention, not a requirement.

The ending sampXML tag

The final fragment of the XML file is the ending tag for the sampXML element as shown in Figure 4.  It is just an ordinary end tag for an element.

</sampXML>

Where is the XSLT File?

Because of the length of the lesson and the amount of detail involved, I decided to split the lesson into two parts.  Part II will be included in a future lesson.

The XSLT file will be discussed in Part II.

Complete Program Listing

A complete listings of the XML file is shown in Figure 5.  See Part II for a listing of the corresponding XSLT file.
<!-- File XSL002.xml
Copyright 2000 R. G. Baldwin
Illustrates for-each construct
Works with IE5.0
-->
<?xml-stylesheet 
  type="text/xsl" 
  href="XSL002.xsl"?>

<sampXML >

<repeat>
<para>This is a paragraph.</para>
</repeat>

<repeat>
<para>This is another paragraph.</para>
</repeat>

</sampXML>

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without  express written permission from Richard Baldwin is prohibited.


About the author:

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

1




Networking Solutions





Partners

  • Partner With Us














More for Developers

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs