X hits on this document

398 views

0 shares

1 downloads

0 comments

44 / 142

Transforming XML with XSLT

Exercise - XML to text, redux

In this exercise you will learn how to make XSLT file a bit more modular and less like CSS files.

Many of the elements of the file ala.xml where intended to be processed similarly. Computer program- mers don't like to do the same thing over and over again. The want to code it once and leave it at that.

Note the following stylesheet:

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

<!-- plain ol' text; get rid of some white space --> <xsl:output method='text'/> <xsl:strip-space elements='*'/>

<!-- let's get started --> <xsl:template match="/letter">

<!-- add a line-feed for formatting's sake --> <xsl:text>&#xa;</xsl:text>

<!-- do the work --> <xsl:apply-templates/>

</xsl:template>

<!-- trap all the various elements --> <xsl:template match='date | address_two | greeting | paragraph | closing'>

<xsl:value-of select='normalize-space(.)'/> <xsl:text>&#xa;&#xa;</xsl:text> </xsl:template>

<xsl:template match='name | address_one'>

<xsl:value-of select='normalize-space(.)'/> <xsl:text>&#xa;</xsl:text> </xsl:template>

<xsl:template match='list'>

<xsl:apply-templates/> <xsl:text>&#xa;</xsl:text> </xsl:template>

<xsl:template match='item'>

<!-- insert a tab, an asterisk, and a space for formatting --> <xsl:text>&#x9;*&#x20;</xsl:text> <xsl:value-of select='normalize-space(.)'/> <xsl:text>&#xa;</xsl:text> </xsl:template>

</xsl:stylesheet>

It works very much like the previous examples but with a number of exceptions:

36

Document info
Document views398
Page views404
Page last viewedMon Dec 05 03:32:55 UTC 2016
Pages142
Paragraphs3126
Words34660

Comments