2010-12-15 4 views
0

すでに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> 

答えて

2

あなたは再び物事を複雑にしています! ;-)

Dimitries優れたソリューションは、別のxsl:analyze-stringを導入せずにニーズに合わせて簡単に調整することができます。

あなたがする必要があるすべては、あなたがmy:replace-wordsを呼び出すとき@name sおよびsynonym秒の労働組合を作るです:

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

次にパラメータsynonymsを削除し、あなたのようにxsl:non-matching-substringxsl:value-ofを使用してそれに応じて機能my:replace-wordsを調整前にした。

であなたの現在の機能my:insert-acronymsを置き換えます、これにより

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

次のXML::で

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

...とあなたの現在のmy:replace-words

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

は、次の結果を返します:

<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

こんにちはパー、迅速かつ有用な答えてくれてありがとう! – Suidu

関連する問題