首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xslt:analyze-string将缩略词添加到HTML -现在具有同义词

使用xslt:analyze-string将缩略词添加到HTML -现在具有同义词
EN

Stack Overflow用户
提问于 2010-12-15 18:37:36
回答 1查看 804关注 0票数 0

我已经发布了如何在HTML中添加首字母缩略词标签的问题,并得到了一个很好的解决方案(参见Use xslt:analyze-string to add acronyms to HTML )。谢谢!

现在,我将同义词添加到我的缩写词中,并调整了解决方案-它工作得很好。

我唯一的问题是:将同义词的xsl:analyze-string指令放在主词(name)的第一个xsl:analyze-string的xsl:non-matching-substring部分中是否有用?有没有其他方法来实现这一点?

下面是我的源码和转换。

感谢您的提示!:-)

Suidu

source.xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-15 20:10:57

你又把事情复杂化了!;-)

Dimitries优秀的解决方案可以很容易地进行调整,以满足您的需求,而无需引入另一个xsl:analyze-string

当您调用my:replace-words时,您需要做的就是将@namesynonym结合起来。

代码语言:javascript
复制
my:replace-words($text, ($acronyms/@name|$acronyms/synonym))

然后,通过删除参数synonyms并像之前一样在xsl:non-matching-substring中使用xsl:value-of,相应地调整函数xsl:non-matching-substring

将当前的my:insert-acronyms 函数替换为:

代码语言:javascript
复制
  <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>

使用以下命令...and您当前的my:replace-words

代码语言:javascript
复制
  <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>

通过执行此操作,执行以下

代码语言:javascript
复制
<?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> 

将返回以下结果:

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4448992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档