TEI Utility stylesheet defining functions for use in all
output formats.
This software is dual-licensed:
1. Distributed under a Creative Commons Attribution-ShareAlike 3.0
Unported License http://creativecommons.org/licenses/by-sa/3.0/
2. http://www.opensource.org/licenses/BSD-2-Clause
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
This software is provided by the copyright holders and contributors
"as is" and any express or implied warranties, including, but not
limited to, the implied warranties of merchantability and fitness for
a particular purpose are disclaimed. In no event shall the copyright
holder or contributors be liable for any direct, indirect, incidental,
special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use,
data, or profits; or business interruption) however caused and on any
theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use
of this software, even if advised of the possibility of such damage.
[common] handle character data.
Following http://wiki.tei-c.org/index.php/XML_Whitespace#XSLT_Normalization_Code,
the algorithm to normalize space in mixed content is:
Collapse all white space, then
trim leading space on the first text node in an element and
trim trailing space on the last text node in an element,
trim both if a text node is both first and last, i.e., is the only text node in the
element.
<xsl:template match="text()" mode="#default plain"><xsl:choose><xsl:when test="ancestor::*[@xml:space][1]/@xml:space='preserve'"><xsl:value-of select="tei:escapeChars(.,parent::*)"/></xsl:when><xsl:otherwise><!-- Retain one leading space if node isn't first, has
non-space content, and has leading space.--><xsl:variable name="context" select="name(parent::*)"/><xsl:if test="matches(.,'^\s') and normalize-space()!=''"><!-- if the text is first thing in a note, zap it, definitely --><xsl:choose><xsl:when test="(tei:isFootNote(..) or tei:isEndNote(..)) and position()=1"/><!-- but if its in a run on inline objects with the same
name (like a sequence of <hi>), then the space needs
keeping --><xsl:when test="(tei:isInline(parent::*) and parent::*/preceding-sibling::node()[1][name()=$context])"><xsl:call-template name="space"/></xsl:when><xsl:when test="position()=1"/><xsl:otherwise><xsl:call-template name="space"/></xsl:otherwise></xsl:choose></xsl:if><xsl:value-of select="tei:escapeChars(normalize-space(.),parent::*)"/><xsl:choose><!-- node is an only child, and has content but it's all space --><xsl:when test="last()=1 and string-length()!=0 and normalize-space()=''"><xsl:call-template name="space"/></xsl:when><!-- node isn't last, isn't first, and has trailing space --><xsl:when test="position()!=1 and position()!=last() and matches(.,'\s$')"><xsl:call-template name="space"/></xsl:when><!-- node isn't last, is first, has trailing space, and has non-space content --><xsl:when test="position()=1 and matches(.,'\s$') and normalize-space()!=''"><xsl:call-template name="space"/></xsl:when></xsl:choose></xsl:otherwise></xsl:choose></xsl:template>
<xsl:function name="tei:findLanguage"><xsl:param name="context"/><!-- note: $context should always be 1 node, so following --><!-- for-each just sets context node and executes once for it --><xsl:for-each select="$context"><xsl:value-of select="(ancestor-or-self::*[@xml:lang][1]/@xml:lang/string(),'en')[1]"/><!--
That XPath is a bit complex, so deserves some explanation.
( = start a (two item) sequence
ancestor-or-self::tei:* = generate sequence of elements starting from the context
node selecting each parent:: until the outermost element
[@xml:lang] = filter the selected set to only those that have @xml:lang; note that
the sequence is still in closest to root order
[1] = take only the first, i.e. closest, of those nodes
/@xml:lang = take its @xml:lang attribute
/string() = convert that attribute to a string
, = separate the two items in our sequence; note that what's on the L will be
either a single @xml:lang value or nothing
'en' = second item in our two item sequence
) = end the (two item) sequence
[1] = select the first item in the sequence: if the first item is nothing, it is
really a one item sequence, we get the 'en'; if the first item is a string
we get it.
--></xsl:for-each><!-- Syd Bauman scripsit--></xsl:function>