Making your string search routines and string parsers re-usable templates
Making search routines and string parsing templates reusable is a crucial part of using your resources wisely. No matter what size your organization is, having a library of well-documented, re-usable templates is a great time-saver.
In fact, the MindMap team loved the br-replace
template (which you saw earlier in the tutorial) so much that they dropped it into
their directory of generic XSLT templates and now use it nearly every day. Generic
templates act as function libraries and should be made in as generic a way as possible
so they can be reused. Consider the template below -- you don't define any
of the matching nodes in this initial template; a good generic template should
be able to stand on its own, callable by any file. This isn't always possible,
but it's always desirable. You should be able to use this template with any
string of text that has breaks in it:
<xsl:template name="links">
<xsl:param name="String" />
<xsl:choose>
<xsl:when test="contains($String,',')">
<a href="#id{substring-before($String,',')}">
<xsl:value-of
select="substring-before($String,',')" />
</a>
<xsl:text></xsl:text>
<xsl:call-template name="links">
<xsl:with-param name="String"
select="substring-after($String,',')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a href="#id{$String}">
<xsl:value-of select="$String" />
</a>
<xsl:text></xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
|
If you're entering text into a text editor, name the file links.xsl or download it from the source package, available in Tools. Then, create a new XSLT file named call-links.xsl, and enter the following code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:include href="links.xsl" />
<xsl:template match="/">
<html>
<head>
<title>Links</title>
</head>
<xsl:apply-templates select="WebConfig" />
</html>
</xsl:template>
<xsl:template match="WebConfig">
<body>
<xsl:call-template name="links">
<xsl:with-param name="String" select="." />
</xsl:call-template>
</body>
</xsl:template>
...
|
Note that not all the code is included here and as the code stands it is not yet well formed. You'll need to either finish it yourself or download the file. As you can see, only the XSLT document doing the calling contains any path information. You can try this on just about any XML document with lots of text. You don't need to change the named template, just the parameters on the calling template.



