Example of <xsl:message>
This example demonstrates use of the <xsl:message>
element.
The style sheet verifies that the <name>
element within a <record>
element has been filled in. If a <name>
element is empty, a message indicating that the XML is invalid is output.
In the XML file, a name is not supplied for the <name>
element in the second record element.
XML File (records.xml)
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="message.xsl" ?>
<records>
<record>
<name>David Perry</name>
<address>222 Cherry</address>
<phone>555-797-79797</phone>
</record>
<record>
<name></name>
<address>312 Elm</address>
<phone>555-797-79797</phone>
</record>
</records>
XSLT File (message.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<xsl:apply-templates select="*"/>
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="//record"/>
</xsl:template>
<xsl:template match="record">
<xsl:if test="name=''">
<xsl:message terminate="yes">A name field is empty.
</xsl:message>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output
The formatted output is the following error message in Internet Explorer:
A name field is empty.
This the processor output:
<?xml version="1.0" encoding="UTF-16"?>