Messages

Many software developers will be familiar with the (now primitive) technique of adding 'print' statments to their programs in order to track down bugs. A similar technique is available to XSLT stylesheet developers, using the Message element. This element can be placed within a template and holds an appropriate message for display when the template is activated:

<template match="//warning/*/para">
  <message>Template //warning/*/para is activated</message>
  <apply-templates/>
</template>

This element has no effect on normal output from the stylesheet. Instead, the text content of the Message element is somehow presented to the user of the XSLT processor software. In a windows-based system, a pop-up window may appear. On a command-line system, the messages would appear as standard output:

D:xslt> xslt In.xml Out.htm
D:xslt> MESSAGE: Template //warning/*/para is activated
D:xslt> FINISHED
D:xslt>

The Terminate attribute, which has a default value of 'no', may be set to 'yes' in order to terminate document processing immediately:

<template match="problem">
  <message terminate="yes">Problem element detected
  correct the error and restart</message>
</template>

The Message element may be used within other elements that a Template element can contain:

<template match="para">
  <if test="@secret='yes'">
    ...
    <message>Secret paragraph found.</message>
  </if>
  ...
</template>

The Message element may in turn contain all these other elements, including the Value Of element, which is particularly useful for reporting on the current context. For example, the title of the chapter containing a secret chapter can be reported:

<template match="para[@secret='yes']">
  <message>Secret paragraph found in Chapter:
    <value-of select="ancestor::chapter/title" />
  </message>
  ...
</template>

As another example, a named template could be created that reports the name and identifier of the current element:

<template name="elementName">
  <message>
    ELEMENT: <value-of select="name()" />
    ID: <value-of select="@id" />
  </message>
</template>

<template match="para">
  <call-template name="elementName" />
  ...
</template>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset