使用 xslt:analyze-string 向 HTML 添加首字母缩略词 [英] Use xslt:analyze-string to add acronyms to HTML

查看:32
本文介绍了使用 xslt:analyze-string 向 HTML 添加首字母缩略词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 xslt:analyze-string 向 HTML 文本添加首字母缩略词.问题:在我的 HTML 文本中有诸如

之类的标签

一些文字

被视为 XML 节点.当我应用 xslt:analyze-string 时,这些节点被转换为字符串——标签被剥离.同样在我的递归 XSLT 样式表中,已经插入的首字母缩略词也被删除了.

我的问题:是否有一个技巧可以防止 xslt:analyze-string 将 HTML 节点转换为字符串并保留 HTML 标签?

这是我的例子:

样式表:

</xsl:模板><xsl:template name="insert-acronyms"><xsl:param name="text" as="node()*"/><xsl:param name="首字母缩略词"/><xsl:when test="$acronyms"><xsl:call-template name="insert-acronyms"><xsl:with-param name="acronyms" select="$acronyms[position() &gt; 1]"/><xsl:with-param name="text"><xsl:call-template name="replace-words"><xsl:with-param name="text" select="$text"/><xsl:with-param name="name" select="$acronyms[1]/name"/><xsl:with-param name="description" select="$acronyms[1]/description"/></xsl:call-template></xsl:with-param></xsl:call-template></xsl:when><xsl:否则><xsl:copy-of select="$text"/></xsl:否则></xsl:选择></xsl:模板><xsl:template name="replace-words"><xsl:param name="text"/><xsl:param name="name"/><xsl:param name="description"/><xsl:analyze-string select="$text" regex="{concat('(^|\W)(', string-join($name, '|'), ')(\W|$)')}"><xsl:匹配子串><xsl:value-of select="regex-group(1)"/><xsl:element name="首字母缩略词"><xsl:attribute name="title"><xsl:value-of select="$description"/></xsl:attribute><xsl:value-of select="regex-group(2)"/></xsl:element><xsl:value-of select="regex-group(3)"/></xsl:matching-substring><xsl:非匹配子串><xsl:value-of select="."/></xsl:非匹配子串></xsl:analyze-string></xsl:模板></xsl:stylesheet>

来源:

<字典><缩写词><name>WWW</name><description>万维网</description></首字母缩略词><缩写词><名称>HTML<description>超文本标记语言</description></首字母缩略词></字典><div><p>在<strong>WWW</strong>你可以找到很多<em>HTML</em></p>

</doc>

转换的结果(strong 和 em 标签被剥离,只插入一个首字母缩略词,因为另一个也被剥离了):

解决方案

提供的代码过于复杂.主要问题是尝试一次创建一个首字母缩略词,并且还不必要地尝试递归处理.

这是一个更简单、更合乎逻辑的非递归解决方案:

<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:模板><xsl:template match="/*"><xsl:apply-templates/></xsl:模板><xsl:template match="text()" priority="0.1"><xsl:序列选择="my:insert-acronyms(.,/*/dictionary/acronym)"/></xsl:模板><xsl:function name="my:insert-acronyms" as="node()*"><xsl:param name="text" as="text()"/><xsl:param name="acronyms" as="node()*"/><xsl:序列选择=如果($首字母缩写词)然后我的:替换词($text, $acronyms/name)否则 $text"/></xsl:function><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:匹配子串><xsl:value-of select="regex-group(1)"/><acronym title="{$names[.eq regex-group(2)]/../description}"><xsl:value-of select="regex-group(2)"/></首字母缩略词><xsl:value-of select="regex-group(3)"/></xsl:matching-substring><xsl:非匹配子串><xsl:value-of select="."/></xsl:非匹配子串></xsl:analyze-string></xsl:function><xsl:template match="dictionary"/></xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<字典><缩写词><name>WWW</name><description>万维网</description></首字母缩略词><缩写词><名称>HTML<description>超文本标记语言</description></首字母缩略词></字典><div><p>在<strong>WWW</strong>你可以找到很多<em>HTML</em></p>

</doc>

产生想要的、正确的结果:

<p>在<strong><acronym title="万维网">WWW</acronym>你可以找到很多<em><acronym title="超文本标记语言">HTML</acronym></em></p>

I want to add acronyms to an HTML text using xslt:analyze-string. The problem: in my HTML text are tags such as

<strong>some text</strong> 

that are treated as XML nodes. When I apply xslt:analyze-string these nodes are converted to strings - the tags are stripped away. Also in my recursive XSLT stylesheet the acronyms that are already inserted are stripped away, too.

My question: is there a trick to prevent xslt:analyze-string of transforming the HTML nodes to strings and preserve the HTML tags?

Here's my example:

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml" >

    <xsl:template match="/">
        <div>
            <xsl:call-template name="insert-acronyms">
                <xsl:with-param name="text" select="/doc/div"/>
                <xsl:with-param name="acronyms" select="/doc/dictionary/acronym"/>
            </xsl:call-template>
        </div>
    </xsl:template>


    <xsl:template name="insert-acronyms">
        <xsl:param name="text" as="node()*"/>
        <xsl:param name="acronyms"/>

        <xsl:choose>
            <xsl:when test="$acronyms">
                <xsl:call-template name="insert-acronyms">
                    <xsl:with-param name="acronyms" select="$acronyms[position() &gt; 1]"/>
                    <xsl:with-param name="text">
                        <xsl:call-template name="replace-words">
                            <xsl:with-param name="text" select="$text"/>
                            <xsl:with-param name="name" select="$acronyms[1]/name"/> 
                            <xsl:with-param name="description" select="$acronyms[1]/description"/>
                        </xsl:call-template>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>


    <xsl:template name="replace-words">
        <xsl:param name="text" />
        <xsl:param name="name" />
        <xsl:param name="description" />

        <xsl:analyze-string select="$text" regex="{concat('(^|\W)(', string-join($name, '|'), ')(\W|$)')}">
            <xsl:matching-substring>
                <xsl:value-of select="regex-group(1)"/>
                <xsl:element name="acronym">
                 <xsl:attribute name="title"><xsl:value-of select="$description"/></xsl:attribute>
                    <xsl:value-of select="regex-group(2)"/>
                </xsl:element>
                <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:template> 

</xsl:stylesheet>

Source:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <dictionary>

        <acronym>
            <name>WWW</name>
            <description>The World Wide Web</description>
        </acronym>

        <acronym>
            <name>HTML</name>
            <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> 

Result of the transformation (strong and em-tags are stripped away, only one acronym is inserted because the other is stripped away, too):

<?xml version="1.0" encoding="UTF-8"?>
<div>    In the WWW you can find a lot of <acronym title="The HyperText Markup Language">HTML</acronym> documents. </div>

解决方案

The provided code is unnecessarily complicated. The main problem is trying to create an acronym one-at-a-time and also unnecessarily trying recursive processing.

Here is a simpler and logical, non-recursive solution:

<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)
       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: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:template match="dictionary"/>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<doc>
    <dictionary>
        <acronym>
            <name>WWW</name>
            <description>The World Wide Web</description>
        </acronym>
        <acronym>
            <name>HTML</name>
            <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>

the wanted, correct result is produced:

<div>
   <p>In the <strong>
         <acronym title="The World Wide Web">WWW</acronym>
      </strong> you can find a lot of <em>
         <acronym title="The HyperText Markup Language">HTML</acronym>
      </em> documents.</p>
</div>

这篇关于使用 xslt:analyze-string 向 HTML 添加首字母缩略词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆