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 xref"><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 of 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: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:makeDescription" as="node()*"><xsl:param name="context"/><xsl:param name="showListRef"/><!-- MDH 2018-01-21: added this param so we can stop building
ugly and superfluous lists in Guidelines ref pages. See
issue #296. --><xsl:param name="makeMiniList" as="xs:boolean"/><xsl:variable name="D"><xsl:for-each select="$context"><xsl:variable name="langs" select="tei:generateDocumentationLang(.)"/><xsl:variable name="firstLang" select="($langs)[1]"/><!-- first the gloss --><xsl:sequence select="tei:makeGloss(.,$langs)"/><!-- now the description --><!--
Change, 2020-02-10 in response to #418:
Only look at, count, or copy those <desc> elements that do
NOT have a @type of "deprecationInfo". Note that we use
not(@type eq 'dI') because using just @type ne 'dI' does not
include those <desc>s that do not have @type at all.
--><xsl:choose><xsl:when test="not(tei:desc[ not( @type eq 'deprecationInfo' ) ])"></xsl:when><xsl:when test="count(tei:desc[ not( @type eq 'deprecationInfo' ) ])=1"><xsl:for-each select="tei:desc[ not( @type eq 'deprecationInfo' ) ]"><xsl:apply-templates select="." mode="inLanguage"/></xsl:for-each></xsl:when><xsl:when test="tei:desc[ not( @type eq 'deprecationInfo' ) ][@xml:lang=$firstLang]"><xsl:for-each select="tei:desc[ not( @type eq 'deprecationInfo' ) ][@xml:lang=$firstLang]"><xsl:apply-templates select="." mode="inLanguage"/></xsl:for-each></xsl:when><xsl:otherwise><xsl:variable name="D"><xsl:for-each select="tei:desc[ not( @type eq 'deprecationInfo' ) ]"><xsl:variable name="currentLang" select="tei:findLanguage(.)"/><xsl:if test="$currentLang=($langs)"><xsl:apply-templates select="." mode="inLanguage"/></xsl:if></xsl:for-each></xsl:variable><xsl:choose><xsl:when test="$D='' and tei:desc[ not( @type eq 'deprecationInfo' ) ][(not(@xml:lang) or @xml:lang='en')]"><xsl:for-each select="tei:desc[ not( @type eq 'deprecationInfo' ) ][(not(@xml:lang) or @xml:lang='en')]"><xsl:apply-templates select="." mode="inLanguage"/></xsl:for-each></xsl:when><xsl:when test="not($oddmode='tei')"><xsl:sequence select="$D/text()"/></xsl:when><xsl:otherwise><xsl:copy-of select="$D"/></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose><xsl:choose><xsl:when test="$oddmode='tei'"/><!--
The original code, which had separate templates for tei:valList[@type=open] and
tei:valList[@type=semi], was very redundant. However, it may have been very clever
in how it handled the case of multiple child <valList>s. Or, it may have obliviously
worked in that case, producing passable, if not ideal, output. Depends on your point
of view, in part.
I believe we reproduce what it did, whether you like it or not, by using '=' instead
of 'eq' in the "if" comparison in the definition of $msg.
—Syd, 2018-01-19
--><!-- MDH 2018-01-21: using $makeMiniList param so we can stop building
ugly and superfluous lists in Guidelines ref pages. See
issue #296. --><xsl:when test="tei:valList[ @type = ('open','semi')] and $makeMiniList = true()"><xsl:variable name="msg" select="tei:i18n( concat( if (tei:valList/@type = 'open') then 'Sample' else 'Suggested', ' values include' ) )"/><xsl:value-of select="concat('
', $msg, ': ')"/><xsl:for-each select="tei:valList/tei:valItem"><xsl:number/><xsl:text>] </xsl:text><xsl:choose><xsl:when test="tei:altIdent=@ident"><xsl:value-of select="@ident"/></xsl:when><xsl:when test="tei:altIdent"><xsl:value-of select="normalize-space(tei:altIdent)"/></xsl:when><xsl:otherwise><xsl:value-of select="@ident"/></xsl:otherwise></xsl:choose><xsl:variable name="langs"><xsl:value-of select="concat(normalize-space(tei:generateDocumentationLang(.)),' ')"/></xsl:variable><xsl:variable name="firstLang" select="($langs)[1]"/><xsl:variable name="gloss" select="normalize-space( string-join( tei:makeGloss(.,$langs),'') )"/><xsl:value-of select="concat( if ($gloss ne '') then ' ' else '', $gloss, if (following-sibling::tei:valItem) then ';' else '', if (position() ne last()) then ' ' else '' )"/></xsl:for-each></xsl:when></xsl:choose><xsl:if test="tei:listRef and $showListRef"><xsl:text> [</xsl:text><xsl:for-each select="tei:listRef/tei:*"><xsl:apply-templates select="." mode="weave"/><xsl:if test="following-sibling::tei:*"><xsl:text></xsl:text></xsl:if></xsl:for-each><xsl:text>]</xsl:text></xsl:if></xsl:for-each></xsl:variable><xsl:copy-of select="$D"/></xsl:function>
<xsl:function name="tei:descOrGlossOutOfDate" as="xs:boolean"><xsl:param name="context"/><xsl:variable name="lang" select="tei:generateDocumentationLang($context)[1]"/><!--
Coding NOTE: The 4 tests that comprise $test_results currently
use “gt” the value operator rather than ‘>’ the general
operator. This is because we expect that there will ever only
be 1 sibling <gloss> or <desc> that has the same @type and
same @xml:lang. But we might be proven wrong, in which case
perhaps ‘>’ would have been better. (If you change it, change
this comment, too!)
--><xsl:variable name="test_results" as="xs:boolean*"><!-- first test <gloss> children without @type --><xsl:sequence select="$context/tei:gloss[ not(@type) ][ @xml:lang eq 'en' ]/@versionDate gt $context/tei:gloss[ not(@type) ][ @xml:lang eq $lang ]/@versionDate"/><!-- second test <desc> children without @type --><xsl:sequence select="$context/tei:desc[ not(@type) ][ @xml:lang eq 'en' ]/@versionDate gt $context/tei:desc[ not(@type) ][ @xml:lang eq $lang ]/@versionDate"/><!-- next test <gloss> children with @type --><xsl:for-each select="distinct-values( $context/tei:gloss/@type )"><xsl:sequence select="$context/tei:gloss[ @type eq . ][ @xml:lang eq 'en' ]/@versionDate gt $context/tei:gloss[ @type eq . ][ @xml:lang eq $lang ]/@versionDate"/></xsl:for-each><!-- last test <desc> children with @type --><xsl:for-each select="distinct-values( $context/tei:desc/@type )"><xsl:sequence select="$context/tei:desc[ @type eq . ][ @xml:lang eq 'en' ]/@versionDate gt $context/tei:desc[ @type eq . ][ @xml:lang eq $lang ]/@versionDate"/></xsl:for-each></xsl:variable><!-- if any one of the above is true, then something is amiss, return true. --><xsl:sequence select="$test_results = true()"/></xsl:function>
<xsl:function name="tei:makeGloss" as="node()*"><xsl:param name="context"/><xsl:param name="langs"/><!-- This function returns a sequence of strings that may include
leading or trailing whitespace. --><xsl:variable name="firstLang" select="($langs)[1]"/><xsl:for-each select="$context"><xsl:choose><xsl:when test="not(tei:gloss)"/><xsl:when test="string-length(tei:gloss[1])=0"/><xsl:when test="count(tei:gloss)=1 and not(tei:gloss[@xml:lang])"><xsl:text>(</xsl:text><xsl:apply-templates select="tei:gloss" mode="inLanguage"/><xsl:text>) </xsl:text></xsl:when><xsl:when test="tei:gloss[@xml:lang=$firstLang]"><xsl:if test="not(tei:gloss[@xml:lang=$firstLang]='')"><xsl:text>(</xsl:text><xsl:apply-templates select="tei:gloss[@xml:lang=$firstLang]" mode="inLanguage"/><xsl:text>) </xsl:text></xsl:if></xsl:when><xsl:otherwise><xsl:variable name="G"><xsl:for-each select="tei:gloss"><xsl:variable name="currentLang" select="tei:findLanguage(.)"/><xsl:if test="$currentLang=($langs)"><xsl:text>(</xsl:text><xsl:apply-templates select="." mode="inLanguage"/><xsl:text>) </xsl:text></xsl:if></xsl:for-each></xsl:variable><xsl:choose><xsl:when test="$G='' and tei:gloss[(not(@xml:lang) or @xml:lang='en')]"><xsl:text>(</xsl:text><xsl:apply-templates select="tei:gloss[(not(@xml:lang) or @xml:lang='en')]" mode="inLanguage"/><xsl:text>) </xsl:text></xsl:when><xsl:otherwise><xsl:copy-of select="$G"/></xsl:otherwise></xsl:choose></xsl:otherwise></xsl:choose></xsl:for-each></xsl:function>
* tei:generate-nsprefix-schematron($e)
* Calculate a namespace prefix for a given element, or fail. *
Namespace
http://www.tei-c.org/ns/1.0
Type
xs:string
Parameters
QName
Namespace
Type
e
No namespace
element()
Import precedence
10
Source
<xsl:function name="tei:generate-nsprefix-schematron" as="xs:string"><xsl:param name="e" as="element()"/><xsl:for-each select="$e"><xsl:variable name="myns" select="normalize-space( ancestor::tei:elementSpec/@ns )"/><xsl:variable name="prefixes" select="in-scope-prefixes(.)" as="xs:string*"/><xsl:variable name="uris" select="for $pre in $prefixes return namespace-uri-for-prefix( $pre,$e)" as="xs:anyURI*"/><xsl:choose><!--* if ns is not specified or is tei, use 'tei'
* Note that $myns will be undefined both when the ancestor
* <elementSpec> does not have an @ns, and when there is no
* ancestor <elementSpec>. *--><xsl:when test="not($myns) or $myns eq 'http://www.tei-c.org/ns/1.0'"><xsl:text>tei:</xsl:text></xsl:when><!--* otherwise, if an ancestor has a suitable <sch:ns> element,
* use the one belonging to the nearest such ancestor *--><xsl:when test="ancestor::*/sch:ns[ normalize-space(@uri) eq $myns ]"><xsl:variable name="a" as="element()" select="ancestor::*[sch:ns[ normalize-space(@uri) eq $myns ]][1]"/><xsl:variable name="prefix" as="xs:string*" select="$a/sch:ns[ normalize-space(@uri) eq $myns ]/normalize-space(@prefix)"/><xsl:value-of select="concat( $prefix[1],':')"/></xsl:when><!--* Otherwise, if the actual declaration has a suitable
* namespace node, use the current prefix. Or, to be
* exact, any one of the current non-null prefixes.
* We'll take the first one presented.
*--><xsl:when test="$uris = $myns and $myns ne ''"><xsl:variable name="indx" select="index-of( $uris, $myns )[1]"/><xsl:value-of select="concat( $prefixes[$indx],':')"/></xsl:when><!--* If no ancestor has a suitable sch:ns child, and we don't
* have any local namespace bindings with non-zero
* prefixes, then we are desperate and we will take any
* sch:ns in the schemaSpec (this is getting a bit desperate) *--><xsl:when test="ancestor::tei:schemaSpec//sch:ns[ normalize-space(@uri) eq $myns ]"><xsl:variable name="NSs" select="ancestor::tei:schemaSpec//sch:ns[ normalize-space(@uri) eq $myns ]"/><xsl:value-of select="concat($NSs[1]/normalize-space(@prefix),':')"/></xsl:when><xsl:otherwise><xsl:message terminate="yes" select="concat('schematron rule cannot work out prefix for ', ancestor::tei:elementSpec/@ident, '.')"/></xsl:otherwise></xsl:choose></xsl:for-each></xsl:function>
<xsl:function name="tei:stylesheetVersion" as="xs:string"><xsl:choose><xsl:when test="$useFixedDate='true'">0</xsl:when><xsl:when test="unparsed-text-available('../VERSION')"><xsl:value-of select="normalize-space( unparsed-text('../VERSION') )"/></xsl:when><xsl:otherwise><xsl:message>WARNING: Unable to determine Stylesheet’s version number because the file ../VERSION was not found; using “0.0.0”.</xsl:message><xsl:value-of select="'0.0.0'"/></xsl:otherwise></xsl:choose></xsl:function>
<xsl:function name="tei:makePatternID" as="xs:string"><xsl:param name="context"/><xsl:for-each select="$context"><xsl:variable name="num"><xsl:number level="any"/></xsl:variable><xsl:value-of select="( ../ancestor::*[@ident]/@ident/translate( .,':',''), 'constraint', ../@ident,local-name(), $num )" separator="-"/><!--
Comment on the 1st XPath in the above sequence (the one
that generates the pre-"constraint" part of the returned
string, a sequence of hyphen-separated @ident attributes):
We remove ':' from the value of each @ident to address
issue #330, which occurs when the value of @ident is
"xml:id" (or anything else with a colon). Note that the
value we are generating has to be an XML NCName. The value
of almost any @ident in the TEI is an XML Name, with two
exceptions: prefixDef/@ident (which can contain '+', and
can even start with '+', '-', or '.') and valItem/@ident
(which can contain *anything*). However, neither
<prefixDef> nor <valItem> have <constraintSpec>s, so we
don't have to worry about those here. If we did, the clever
XSLT 1.0 version of that XPath is
../ancestor::*[@ident]/@ident/translate( ., translate( .,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-',''),'')
That is probably blazingly fast, but (besides being hard to
read and understand) has the disadvantage that it
inappropriately nukes any NameChar that is not explicitly
listed, like 'é'. Although not as clever and probably
slower, it would be better to use just
../ancestor::*[@ident]/@ident/replace( .,'[^\c]|:','')
if we ever have to worry about any character, not just colon.
Note: I think "replace( .,'\C|:','')" should work, but it did
not in my little test with Saxon-HE 9.8.0.11J.
—Syd, 2018-09-25
--></xsl:for-each></xsl:function>
<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>