<!---->
<
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="
xs tps">
<
xd:
doc type="
stylesheet">
<
xd:
short>
Contains logic for mapping text</
xd:
short>
<
xd:
cvsId>
$Revision$</
xd:
cvsId>
</
xd:
doc>
<
xsl:
variable name="
text.keeps.match.patterns"
select="
tps:get-text-keeps-match-patterns($text.keeps.mapping)"
as="
xs:string*" />
<
xsl:
variable name="
text.keeps.replace.patterns"
select="
tps:get-text-keeps-replace-patterns($text.keeps.mapping)"
as="
xs:string*" />
<
xsl:
variable name="
text.keeps.flags"
select="
tps:get-text-keeps-flags($text.keeps.mapping)"
as="
xs:string*" />
<
xsl:
function name="
tps:get-text-keeps-mapping-result"
as="
xs:string">
<
xsl:
param name="
string"
as="
xs:string" />
<
xsl:
sequence select="
tps:apply-text-keep-rule($string, $text.keeps.match.patterns, $text.keeps.replace.patterns, $text.keeps.flags)" />
</
xsl:
function>
<
xsl:
function name="
tps:apply-text-keep-rule"
as="
xs:string">
<
xsl:
param name="
text"
as="
xs:string" />
<
xsl:
param name="
match-patterns"
as="
xs:string*" />
<
xsl:
param name="
replace-patterns"
as="
xs:string*" />
<
xsl:
param name="
flags"
as="
xs:string*" />
<
xsl:
variable name="
processed-text"
select="
replace($text, $match-patterns[1], $replace-patterns[1], $flags[1])" />
<
xsl:
sequence select="
if ($match-patterns[1]) then tps:apply-text-keep-rule($processed-text, $match-patterns[position() > 1], $replace-patterns[position() > 1], $flags[position() > 1]) else $text" />
</
xsl:
function>
<
xsl:
function name="
tps:get-text-keeps-match-patterns"
as="
xs:string*">
<
xsl:
param name="
mapping"
as="
item()" />
<
xsl:
variable name="
match-patterns"
as="
xs:string*">
<
xsl:
for-each select="
$mapping/action">
<
xsl:
variable name="
pattern"
as="
xs:string*">
<
xsl:
apply-templates select="
."
mode="
text-keeps-match-pattern" />
</
xsl:
variable>
<
xsl:
sequence select="
string-join($pattern,'')" />
</
xsl:
for-each>
</
xsl:
variable>
<
xsl:
sequence select="
$match-patterns" />
</
xsl:
function>
<
xsl:
template match="
find[not(preceding-sibling::*[1][self::find])]"
mode="
text-keeps-match-pattern"
priority="
20">
<
xsl:
call-template name="
first-text-keep-match" />
</
xsl:
template>
<
xsl:
template match="
find[preceding-sibling::*[1][self::find]]"
mode="
text-keeps-match-pattern">
<
xsl:
call-template name="
following-text-keep-match" />
</
xsl:
template>
<
xsl:
template match="
find[not(following-sibling::*[1][self::find])]"
mode="
text-keeps-match-pattern"
priority="
10">
<
xsl:
call-template name="
last-text-keep-match" />
</
xsl:
template>
<
xsl:
template match="
find"
mode="
text-keeps-match-pattern">
<
xsl:
apply-templates select="
@arabic, @roman, @literal, @text"
mode="
text-keeps-match-pattern" />
</
xsl:
template>
<
xsl:
template match="
find/@*[position()!=1]"
mode="
text-keeps-match-pattern"
priority="
20">
<
xsl:
call-template name="
following-text-keep-match" />
</
xsl:
template>
<
xsl:
template match="
@arabic[.='yes']"
mode="
text-keeps-match-pattern">
<
xsl:
text>
\d+</
xsl:
text>
</
xsl:
template>
<
xsl:
template match="
@roman[.='yes']"
mode="
text-keeps-match-pattern">
<
xsl:
text>
[IVX]</
xsl:
text>
</
xsl:
template>
<
xsl:
template match="
@literal[.='yes']"
mode="
text-keeps-match-pattern">
<
xsl:
text>
\w</
xsl:
text>
</
xsl:
template>
<
xsl:
template match="
replace[not(preceding-sibling::*[1][self::replace])]"
mode="
text-keeps-match-pattern"
priority="
20">
<
xsl:
call-template name="
first-text-keep-match" />
</
xsl:
template>
<
xsl:
template match="
replace[not(following-sibling::*[1][self::replace])]"
mode="
text-keeps-match-pattern"
priority="
10">
<
xsl:
call-template name="
last-text-keep-match" />
</
xsl:
template>
<
xsl:
template match="
replace"
mode="
text-keeps-match-pattern">
<
xsl:
apply-templates select="
@text"
mode="
text-keeps-match-pattern" />
</
xsl:
template>
<
xsl:
template match="
@text"
mode="
text-keeps-match-pattern">
<
xsl:
value-of select="
replace(., '\+','\\+')" />
</
xsl:
template>
<
xsl:
template name="
first-text-keep-match">
<
xsl:
text>
(</
xsl:
text>
<
xsl:
next-match />
</
xsl:
template>
<
xsl:
template name="
following-text-keep-match">
<
xsl:
text>
|</
xsl:
text>
<
xsl:
next-match />
</
xsl:
template>
<
xsl:
template name="
last-text-keep-match">
<
xsl:
next-match />
<
xsl:
text>
)</
xsl:
text>
</
xsl:
template>
<
xsl:
function name="
tps:get-text-keeps-replace-patterns"
as="
xs:string*">
<
xsl:
param name="
mapping"
as="
item()" />
<
xsl:
variable name="
replace-patterns"
as="
xs:string*">
<
xsl:
for-each select="
$mapping/action">
<
xsl:
variable name="
pattern"
as="
xs:string*">
<
xsl:
apply-templates select="
."
mode="
text-keeps-replace-pattern" />
</
xsl:
variable>
<
xsl:
sequence select="
string-join($pattern,'')" />
</
xsl:
for-each>
</
xsl:
variable>
<
xsl:
sequence select="
$replace-patterns" />
</
xsl:
function>
<
xsl:
template match="
find[not(preceding-sibling::*[1][self::find])]"
mode="
text-keeps-replace-pattern">
<
xsl:
text>
$</
xsl:
text>
<
xsl:
value-of select="
1 + count(distinct-values(for $s in preceding-sibling::* return local-name($s)))" />
</
xsl:
template>
<
xsl:
template match="
replace"
mode="
text-keeps-replace-pattern">
<
xsl:
apply-templates select="
@with"
mode="
text-keeps-replace-pattern" />
</
xsl:
template>
<
xsl:
function name="
tps:get-text-keeps-flags"
as="
xs:string*">
<
xsl:
param name="
mapping"
as="
item()" />
<
xsl:
sequence select="
for $a in $mapping/action return if ($a/@match-case='yes') then '' else 'i'" />
</
xsl:
function>
</
xsl:
stylesheet>
v