X hits on this document

376 views

0 shares

1 downloads

0 comments

45 / 142

Transforming XML with XSLT

1.

The use of the strip-space eliminates extraneous spaces within elements

2.

The use of apply-templatges is very important. When this element is encountered the XSLT pro- cessor transverses the XML input for elements. When it finds them the processor looks for a match- ing template containing the current element. If it finds a matching template, then processing is done within the template element. Otherwise the element's content is output.

3.

Notice how the date, address two, greeting, paragraph, and closing elements are all processed the same.

_

4.

Notice how the name and adress_one elements are processed the same

5.

The list and item elements are a bit tricky. The list element is first trapped and then the item ele- ment is rendered. Processing then returns to the template for list elements and a simple line feed is output

Transform letter.xml with letter2text.xsl:

  • 1.

    Open a command prompt to the getting-started directory of the workbook's CD.

  • 2.

    Transform letter.xml: xsltproc letter2txt.xsl letter.xml .

Exercise - Transform an XML document into XHTML

Below is our first XSLT/XHTML example. Designed to be applied against the file named letter.xml, it will output a valid XHTML file. You can see this in action by using an XSLT processor named xsltproc. Assuming all the necessary files exist in the same directory, the xstlproc command is xsltproc -o let- ter.html letter2html.xsl letter.xml .

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

<!-- letter2html.xsl; an XSL file -->

<!-- define the output as an XML file, specficially, an XHTML file --> <xsl:output method="xml" omit-xml-declaration="no" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<!-- start at the root of the file, letter --> <xsl:template match="letter">

<!-- output an XHTML root element --> <html>

<!-- open the XHTML's head element --> <head>

<!-- output a title element with the addressee's name -->

37

Document info
Document views376
Page views382
Page last viewedSat Dec 03 15:58:58 UTC 2016
Pages142
Paragraphs3126
Words34660

Comments