すでにHTMLテキストに頭字語のタグを追加する方法を質問したところ、良い解決策が得られました(Use xslt:analyze-string to add acronyms to HTML参照)。ありがとうございました!xslt:analyze-stringを使用して、HTMLに略語を追加する - 同義語付き
私は頭字語に同義語を加え、解決策を適用しました。これはうまくいきます。
私の唯一の質問:メインワード(名前)の最初のxsl:analyze-stringのxsl:non-matching-substring部分の中に同義語のxsl:analyze-string命令を置くと便利ですか? これを実装する他の方法はありますか?
私のソースと変換の下。
あなたのヒントをありがとう! :-)
Suidu
source.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>
transformation.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>
こんにちはパー、迅速かつ有用な答えてくれてありがとう! – Suidu