<!--

# =============================================================================

# Copyright © 2016 Typéfi Systems. All rights reserved.

#

# Unless required by applicable law or agreed to in writing, software

# is distributed on an "as is" basis, without warranties or conditions of any

# kind, either express or implied.

# =============================================================================

-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tps="http://www.typefi.com/ContentXML" xmlns:xd="http://www.pnp-software.com/XSLTdoc" version="2.0" exclude-result-prefixes="#all">





    
    <xd:doc type="stylesheet">

        
<xd:short>

            Contains all auxiliary and general-purpose functions which cannot be attributed to one particular module.

        
</xd:short>

        
<xd:detail>

            If a function serves for purpose that has relation to one of the existing modules then it should be placed into that module,

            e.g. 
<code>tps:get-graphic-file-path</code> function is found in the module <em>jats-graphics.xsl</em>.

        
</xd:detail>

        
<xd:cvsId>   $Revision$</xd:cvsId>

    
</xd:doc>






    
<xsl:function name="tps:non-empty-nodes" as="node()*">

        
<xsl:param name="nodes" as="node()*" />



        
<xsl:sequence select="$nodes[name() or normalize-space()]" />

    
</xsl:function>





    
<xsl:function name="tps:is-non-empty-regular-expression" as="xs:boolean">

        
<xsl:param name="regex" as="xs:string" />



        
<xsl:sequence select="string-length($regex) gt 0" />

    
</xsl:function>





    
<!--

        @Description:   Replace multiple tokens in a string with given values.



        @Note:          The number of tokens should be equal to the number of replacements.



        @Example:       tps:replace-tokens('_T1_ and __T2', ('_T1_', '_T2_'), ('A', 'B'))

                        will return 'A and B'.

        
-->

    

    

    
<xsl:function name="tps:replace-tokens" as="xs:string">

        
<xsl:param name="string" as="xs:string" />

        
<xsl:param name="tokens" as="xs:string+" />

        
<xsl:param name="replacements" as="xs:string+" />



        
<xsl:variable name="regex" select="string-join(for $token in $tokens return concat('(', $token, ')'), '|')" />



        
<xsl:variable name="result">

            
<xsl:analyze-string select="$string" regex="{$regex}">

                
<xsl:matching-substring>

                    
<xsl:sequence select="for $i in 1 to count($tokens) return if (regex-group($i)) then $replacements[$i] else ()" />

                
</xsl:matching-substring>

                
<xsl:non-matching-substring>

                    
<xsl:value-of select="." />

                
</xsl:non-matching-substring>

            
</xsl:analyze-string>

        
</xsl:variable>



        
<xsl:sequence select="string-join($result, '')" />

    
</xsl:function>





    
<xsl:function name="tps:get-build-details" as="xs:string">

        
<xsl:sequence select="tps:replace-tokens($build.details.format, ('__STYLESHEET__', '__CUSTOMER-REVISION-NUMBER__', '__CORE-REVISION-NUMBER__', '__DATETIME__'), ($build.entry.point.stylesheet, $build.customer.revision.number, $build.core.revision.number, $build.datetime))" />

    
</xsl:function>





    
<xsl:function name="tps:format-month" as="xs:string">

        
<xsl:param name="month" as="xs:string" />

        
<xsl:variable name="month-number" select="number($month)" as="xs:double" />



        
<xsl:sequence select="if (not($month castable as xs:double) or $month-number lt 1 or $month-number gt 12) then $month else tps:format-month-number($month-number)" />

    
</xsl:function>





    
<xsl:function name="tps:format-month-number" as="xs:string">

        
<xsl:param name="month" as="xs:double" />



        
<xsl:variable name="date" select="xs:date(concat('2000-', format-number($month, '00'), '-01'))" as="xs:date" />

        
<xsl:variable name="month-string" select="format-date($date, '[MNn]')" as="xs:string" />



        
<xsl:sequence select="(: Remove [Language: en] prefix. :) replace($month-string, '\[.*\]','')" />

    
</xsl:function>





    
<!--

        @Description:   Returns the a mapped value from the specified mapping. The mapping should

                        be represented as a sequence of map elements containing @value and @to

                        attributes.



        @Note:          If there are two (and more) elements containing exactly the same @value,

                        only the first item will be taken into account.

        
-->

    
<xsl:function name="tps:get-mapped-value" as="xs:string?">

        
<xsl:param name="input" as="xs:string?" />

        
<xsl:param name="mapping" as="element(map)*" />



        
<xsl:sequence select="($mapping[@value eq $input]/@to)[1]" />

    
</xsl:function>





    
<!--

        @Description:   Generates an attribute if the value is specified.

        
-->

    
<xsl:function name="tps:create-attribute" as="attribute()?">

        
<xsl:param name="name" as="xs:string" />

        
<xsl:param name="value" as="xs:string?" />



        
<xsl:variable name="result" as="attribute()?">

            
<xsl:if test="exists($value)">

                
<xsl:attribute name="{$name}" select="$value" />

            
</xsl:if>

        
</xsl:variable>



        
<xsl:sequence select="$result" />

    
</xsl:function>





    
<xsl:function name="tps:is-subsequence" as="xs:boolean">

        
<xsl:param name="seq1" as="xs:string+" />

        
<xsl:param name="seq2" as="xs:string+" />



        
<xsl:sequence select="empty(tps:sequence-except($seq1, $seq2))" />

    
</xsl:function>





    
<xsl:function name="tps:sequence-except" as="xs:string*">

        
<xsl:param name="seq1" as="xs:string+" />

        
<xsl:param name="seq2" as="xs:string+" />



        
<xsl:sequence select="distinct-values($seq1[not(. = $seq2)])" />

    
</xsl:function>

    

    

    
<xsl:function name="tps:encode-gt-lt-url" as="xs:string"> 

        
<xsl:param name="url" as="xs:string" />

        

        
<xsl:variable name="ref-val" select="replace(replace($url,'&lt;','%3C'),'&gt;','%3E')" />

        

        
<xsl:sequence select="$ref-val" />

    
</xsl:function>



</xsl:stylesheet>













































































v