Deleting Content

Deleting Content

If your rule for a node does nothingthat is, its emptythe content of the matched node is not copied to the output document. In this way, you can selectively remove content from the source document when you write the output document.

Imagine that you wanted to remove all data about the various planets in planets.xml except for their names and masses. Heres a stylesheet that does the trick:

Listing 3.7 Deleting Content
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:strip-space elements="*"/>  <xsl:output method="xml" indent="yes"/>  <xsl:template match="/">           <xsl:apply-templates/>  </xsl:template>      <xsl:template match="PLANETS">          <xsl:apply-templates/>      </xsl:template>      <xsl:template match="PLANET">         <xsl:copy>            <xsl:apply-templates/>         </xsl:copy>     </xsl:template>      <xsl:template match="NAME">         <xsl:copy>            <xsl:apply-templates/>         </xsl:copy>     </xsl:template>      <xsl:template match="MASS">          <xsl:copy>          <xsl:value-of select="."/>          <xsl:value-of select="@UNITS"/>          </xsl:copy>      </xsl:template>      <xsl:template match="RADIUS">      </xsl:template>      <xsl:template match="DAY">      </xsl:template>      <xsl:template match="DENSITY">      </xsl:template>      <xsl:template match="DISTANCE">      </xsl:template>  </xsl:stylesheet> 

And heres the result document (note that all that Ive preserved are the <NAME> and <MASS> elements):

 <?xml version="1.0" encoding="UTF-8"?>  <PLANET>      <NAME>Mercury</NAME>      <MASS>.0553(Earth = 1)</MASS>  </PLANET>  <PLANET>      <NAME>Venus</NAME>      <MASS>.815(Earth = 1)</MASS>  </PLANET>  <PLANET>      <NAME>Earth</NAME>      <MASS>1(Earth = 1)</MASS>  </PLANET> 

In this way, you can filter XML documents, creating new XML documents with just the data you want.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net