Chapter 6. Transforming from XML to XML, HTML, XHTML, RTF, and Modifying Document Content

Chapter 6. Transforming from XML to XML, HTML, XHTML, RTF, and Modifying Document Content


Your companys online marketing site is powered by XML-based Commerce One software, which uses the Java Message Service (JMS) for secure communications over the Internet. Youve been so successful that youve just taken over your rival. Unfortunately, your former rival uses another XML-based product for its online marketing site, RosettaNet. How can you translate between a Commerce One xCBL purchase order written in XML into a RosettaNet purchase orderalso written in XML, but using an entirely different dialect ?

You use XSLT, of course. XML-to-XML transformations like this are becoming more and more common. In fact, more and more companies are using JMS for secure communications over the Internet, and because JMS runs in Java, its perfect to interface to Java-based XSLT processors such as Xalan or Saxon.

Youve been transforming XML to HTML, XML, and plain text already in this book, but this chapter looks at the process in more depth. Youll also see a new type of transformation in this chapter: XML to JavaScript. In Chapter 10, youll see XML-to-SQL-based database transformations, and in Chapter 11, youll also see XML-to-XSL-FO transformations.

A great deal of XSLT is not just about substituting one element for another, but about completely reorganizing an XML documents content. For example, you might want to reorganize planets.xml in terms of planet density, using XSLT to create a new XML document this way:

 <?xml version="1.0" encoding="UTF-8"?>  <DATA>      <DENSITY>          <VALUE>.983</VALUE>          <NAME>Mercury</NAME>          <MASS>.0553</MASS>          <DAY>58.65</DAY>          <RADIUS>1516</RADIUS>      </DENSITY>      <DENSITY>          <VALUE>.943</VALUE>          <NAME>Venus</NAME>          <MASS>.815</MASS>          <DAY>116.75</DAY>          <RADIUS>3716</RADIUS>      </DENSITY>      <DENSITY>          <VALUE>1</VALUE>          <NAME>Earth</NAME>          <MASS>1</MASS>          <DAY>1</DAY>          <RADIUS>2107</RADIUS>      </DENSITY>  </DATA> 

In fact, this chapter includes a transformation that changes the content in planets.xml completely, leaving only a little HTML and some JavaScript to display a few buttons in a Web browser.

So far in this book, youve only created new elements using literal result elementsthat is, by treating the new elements as text and embedding them into a stylesheet. But as youll see in this chapter, its not always possible to know the names of the new elements you want to create. You can cobble together elements created on the fly, treating them as raw text, but thats fairly crude because it treats markup as text. In this chapter, youll start using the XSLT <xsl:element> , <xsl:attribute> , <xsl:processing-instruction> , and <xsl:comment> elements to create new elements, attributes, processing instructions, and comments at run time. A thorough knowledge of these elements is essential when you start reorganizing XML content.

Youll also see how to use XSLT modes for multiple transformation passes on a document, and how to apply only one of multiple matching templates.

Much of this chapter explores the possibilities of the <xsl:output> element, and Ill start with that element to provide some overview.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: