X hits on this document

382 views

0 shares

1 downloads

0 comments

43 / 142

Transforming XML with XSLT

<xsl:template match="/letter">

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

<!-- address --> <xsl:value-of select='normalize-space(addressee/name)'/> <xsl:text>&#xa;</xsl:text> <xsl:value-of select='normalize-space(addressee/address one)'/> <xsl:text>&#xa;</xsl:text> <xsl:value-of select='normalize-space(addressee/address two)'/> <xsl:text>&#xa;&#xa;</xsl:text> _ _

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

<!-- paragraphs --> <xsl:for-each select='paragraph'>

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

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

</xsl:template> </xsl:stylesheet>

Here is how the stylesheet works:

  • 1.

    Like before, the file is denoted as an XML file and specifically an XSLT stylesheet.

  • 2.

    Like before, output is defined as plain o' text.

  • 3.

    The root of the XML to be transformed is located.

  • 4.

    The stylesheet outputs a line feed, outputs the value of the date element while removing extraneous

white space, and outputs two more line feeds.

  • 5.

    The stylesheet continues in this fashion for the address and greeting elements.

  • 6.

    The stylesheet loops through each paragraph element, normalizes the content it finds, and ouputs

line feeds after each one.

7.

The stylesheet finishes by reading the closing element, and then closing all opened elements.

Try using the stylesheet:

  • 1.

    Open a command prompt and change directories to the getting-started directory.

  • 2.

    Transform the document: xsltproc ala2txt.xsl ala.xml .

35

Document info
Document views382
Page views388
Page last viewedSat Dec 03 23:16:11 UTC 2016
Pages142
Paragraphs3126
Words34660

Comments