Removing Content

Contact Us or call 1-877-932-8228
Removing Content

Removing Content

As the XSLT processor navigates through a source XML document, it looks for nodes that match templates in the XSLT document. If it finds a node that has no matching template, it uses the default XSLT template, which usually outputs the text inside the element and continues its search.

To illustrate this, let's look at TemplatesParametersVariables/Demos/FirstName.xml and TemplatesParametersVariables/Demos/FirstName.xsl below.

Code Sample:

TemplatesParametersVariables/Demos/FirstName.xml
<?xml version="1.0"?>
<?xml-stylesheet href="FirstName.xsl" type="text/xsl"?>
<person>
	<name>
		<firstname>Paul</firstname>
		<lastname>McCartney</lastname>
	</name>
	<job>Singer</job>
	<gender>Male</gender>
</person>

Code Sample:

TemplatesParametersVariables/Demos/FirstName.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text"/>
	<xsl:template match="firstname">
		We found a first name! 
	</xsl:template>
</xsl:stylesheet>

The XSLT processor starts at the top and navigates its way through the XML document looking for templates for each node. However, this XSLT only has a template for the firstname node. So, when the XSLT processor finds the other nodes, it simply outputs the contained text. The result is shown below.

We found a first name! McCartneySingerMale

We must explicitly tell the XSLT processor not to output anything for the lastname, job and gender elements if we want their values not to appear. This is accomplished by creating an empty template matching these elements as shown below.

Code Sample:

TemplatesParametersVariables/Demos/FirstNameOnly.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text"/>
	<xsl:template match="firstname">
		We found a first name! 
	</xsl:template>
	<xsl:template match="lastname | job | gender"/>
</xsl:stylesheet>

Notice that the last xsl:template is an empty tag (i.e, it doesn’t contain anything). This tells the XSLT processor that when the elements indicated by the match attribute are found, nothing should be output. The pipes (|) are uses to separate XPaths.

Next