Markup Utility: Example

The Markup Utility is a utility for finding and changing words and phrases within some text. You can use it to:

This page gives a simple example of using the Markup Utility to markup some text. Say you had some text like:

<p>
  A transformation expressed in XSLT describes rules for transforming a source tree into
  a result tree. The transformation is achieved by associating patterns with templates. A
  pattern is matched against elements in the source tree. A template is instantiated to
  create part of the result tree. The result tree is separate from the source tree. The
  structure of the result tree can be completely different from the structure of the source
  tree. In constructing the result tree, elements from the source tree can be filtered and
  reordered, and arbitrary structure can be added.
</p>

And had some keywords that you wanted to emphasise within the text, defined in XML within the same document:

<keywords>
  <keyword>transformation</keyword>
  <keyword>XSLT</keyword>
  <keyword>source tree</keyword>
  <keyword>result tree</keyword>
  <keyword>template</keyword>
</keywords>

First, you need to import the markup stylesheet into your stylesheet:

<xsl:import href="markup.xsl" />

Second, you need to call the 'markup' template on the text that you want to markup (i.e. the content of the 'p' element. The phrases to be marked up are the keywords that are specified within the text. You can change any of the options that you want to markup all occurences of the keyword, for example:

<xsl:template match="p">
  <p>
    <xsl:call-template name="markup">
      <xsl:with-param name="text" select="." />
      <xsl:with-param name="phrases" select="/doc/keywords/keyword" />
      <xsl:with-param name="first-only" select="false()" />
    </xsl:call-template>
  </p>
</xsl:template>

Finally, you need to declare a template with the 'markup' mode that matches the 'keyword' element and wraps the content of the 'word' parameter within an 'em' element:

<xsl:template match="keyword" mode="markup">
  <xsl:param name="word" />
  <em>
    <xsl:value-of select="$word" />
  </em>
</xsl:template>

This gives the result:

<p>
  A <em>transformation</em> expressed in <em>XSLT</em> describes rules for transforming a <em>source tree</em> into
  a <em>result tree</em>. The <em>transformation</em> is achieved by associating patterns with templates. A
  pattern is matched against elements in the <em>source tree</em>. A <em>template</em> is instantiated to
  create part of the <em>result tree</em>. The <em>result tree</em> is separate from the <em>source tree</em>. The
  structure of the <em>result tree</em> can be completely different from the structure of the source
  tree. In constructing the result tree, elements from the <em>source tree</em> can be filtered and
  reordered, and arbitrary structure can be added.
</p>

/xslt/utilities/markup-example.xml by Jeni Tennison; generated using SAXON 6.5 from Michael Kay