2010-12-15 3 views
0

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> 

Antwort

2

Sie Dinge wieder verkompliziert! ;-)

Dimitries ausgezeichnete Lösung könnte leicht auf Ihre Bedürfnisse angepasst werden, ohne eine andere xsl:analyze-string einzuführen.

Alles was Sie tun müssen, ist eine Vereinigung der @name s zu machen und synonym s, wenn Sie my:replace-words nennen:

my:replace-words($text, ($acronyms/@name|$acronyms/synonym)) 

Dann die Funktion einstellen my:replace-words entsprechend durch den Parameter synonyms zu entfernen und mit xsl:value-of in xsl:non-matching-substring, wie Sie war vorher.

Ersetzen Sie Ihre aktuelle Funktion my:insert-acronyms mit:

<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> 

... und Ihr aktuelles my:replace-words mit:

<xsl:function name="my:replace-words" as="node()*"> 
    <xsl:param name="text" as="text()" /> 
    <xsl:param name="names" 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:value-of select="."/> 
     </xsl:non-matching-substring> 
    </xsl:analyze-string> 
    </xsl:function> 

Damit verfolgt, die folgende 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> 
    <acronym name="XSLT"> 
     <synonym>xslt</synonym> 
     <description>Extensible Stylesheet Language Transformations</description> 
    </acronym> 
    </dictionary> 
    <div> 
    <p>In the <strong>www</strong> you can xslt find a lot of <em>html</em> documents.</p> 
    </div> 
</doc> 

Wird das folgende Ergebnis zurück:

<div> 
    <p>In the <strong> 
     <acronym title="The World Wide Web">www</acronym> 
    </strong> you can <acronym title="Extensible Stylesheet Language Transformations">xslt</acronym> find a lot of <em> 
     <acronym title="The HyperText Markup Language">html</acronym> 
    </em> documents.</p> 
</div> 
+0

Hallo Per, vielen Dank für die schnelle und hilfreiche Antwort! – Suidu

Verwandte Themen