Ich habe bereits die Frage gestellt, wie Akronym-Tags zu HTML-Text hinzufügen und bekam eine schöne Lösung (siehe Use xslt:analyze-string to add acronyms to HTML). Vielen Dank!Verwenden Sie xslt: analyze-string, um Akronyme zu HTML hinzuzufügen - jetzt mit Synonymen
Jetzt habe ich Synonyme zu meinen Akronymen hinzugefügt und die Lösung angepasst - es funktioniert gut.
Meine einzige Frage: ist es sinnvoll, die xsl: analyze-string-Anweisung für die Synonyme innerhalb des xsl: non-matching-substring-Teils der ersten xsl: analyze-string des Hauptworts (name) zu setzen? Gibt es andere Möglichkeiten, dies zu implementieren?
Unter meiner Quelle und Transformation.
Danke für Ihre Hinweise! :-)
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>
Hallo Per, vielen Dank für die schnelle und hilfreiche Antwort! – Suidu