Я уже разместил вопрос о том, как добавить теги акронимов в текст HTML, и получил хорошее решение (см. -html">Используйте xslt:analyze-string для добавления сокращений в HTML ). Спасибо!
Теперь я добавил синонимы к своим аббревиатурам и адаптировал решение - работает нормально.
Мой единственный вопрос: полезно ли помещать инструкцию xsl:analyze-string для синонимов внутри части xsl:non-matching-substring первой строки xsl:analyze-string основного слова (имени)? Есть ли другие способы реализовать это?
Ниже мой источник и трансформация.
Спасибо за ваши подсказки! :-)
Суйду
исходный файл.xml:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<dictionary>
<acronym name="WWW">
<synonym>www</synonym>
<description>The World Wide Web</description>
</acronym>
<acronym name="HTML">
<synonym>html</synonym>
<description>The HyperText Markup Language</description>
</acronym>
</dictionary>
<div>
<p>In the <strong>www</strong> you can find a lot of <em>html</em> documents.</p>
</div>
</doc>
преобразование.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" exclude-result-prefixes="my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()" priority="0.1">
<xsl:sequence select="my:insert-acronyms(., /*/dictionary/acronym)"/>
</xsl:template>
<xsl:function name="my:insert-acronyms" as="node()*">
<xsl:param name="text" as="text()"/>
<xsl:param name="acronyms" as="node()*"/>
<xsl:sequence select=
"if($acronyms)
then my:replace-words($text, $acronyms/@name, $acronyms/synonym)
else $text
"/>
</xsl:function>
<xsl:function name="my:replace-words" as="node()*">
<xsl:param name="text" as="text()" />
<xsl:param name="names" as="node()*" />
<xsl:param name="synonyms" as="node()*" />
<xsl:analyze-string select="$text"
regex="{concat('(^|\W)(', string-join($names, '|'), ')(\W|$)')}">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<acronym title="{$names[. eq regex-group(2)]/../description}">
<xsl:value-of select="regex-group(2)"/>
</acronym>
<xsl:value-of select="regex-group(3)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="."
regex="{concat('(^|\W)(', string-join($synonyms, '|'), ')(\W|$)')}">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<acronym title="{$synonyms[. eq regex-group(2)]/../description}">
<xsl:value-of select="regex-group(2)"/>
</acronym>
<xsl:value-of select="regex-group(3)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:template match="dictionary"/>
</xsl:stylesheet>