2010-12-15 3 views
0

나는 이미 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> 

답변

2

당신은 다시 일을 복잡하게하고 있습니다! ;-)

Dimitries 우수한 솔루션은 다른 xsl:analyze-string을 도입하지 않고도 사용자의 요구에 맞게 쉽게 조정할 수 있습니다. 당신으로 xsl:non-matching-substring에서 매개 변수 synonyms을 제거하고 xsl:value-of를 사용하여

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

가 그런 기능 my:replace-words를 따라 조정 :

당신이해야 할 모든 당신이 my:replace-words를 호출 할 때 a를 @name의의 조합과 synonym들 할 것입니다 전에 했어.

<?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> 
이, 다음과 같은 XML을함으로써

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

:와

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

함께 현재 기능 my:insert-acronyms 교체

는 다음과 같은 결과를 반환합니다

<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