나는 이미 HTML 텍스트에 약어 태그를 추가하는 방법에 대한 질문을 올렸으며 좋은 해결책을 얻었습니다 (Use xslt:analyze-string to add acronyms to HTML 참조). 고맙습니다!xslt : analyze-string을 사용하여 HTML에 약어를 추가하십시오 - 동의어와 함께
이제 두문 약어에 동의어를 추가하고 해결책을 적용했습니다. 제대로 작동합니다.
유일한 질문 : 동의어에 대한 xsl : analyze-string 명령어를 기본 단어 (이름)의 첫 번째 xsl : analyze-string의 xsl : 일치하지 않는 부분 문자열 부분에 두는 것이 유용합니까? 다른 방법으로 구현할 수 있습니까?
내 원본 및 변형 아래.
힌트를 보내 주셔서 감사합니다. :-)
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