xml - 使用 xslt :analyze-string to add acronyms to HTML

标签 xml xslt xslt-2.0

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

之类的标签
<strong>some text</strong> 

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

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

这是我的例子:

样式表:

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

来源:

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

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

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

最佳答案

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

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

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

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

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

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

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

关于xml - 使用 xslt :analyze-string to add acronyms to HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4445567/

相关文章:

java - 使用 xslt 将文本转换为 xml

xslt - xsl : Can a named template return a node list?

xml - 将 XML 注释添加到由 LINQ to SQL 设计器生成的类属性

windows - 为什么 UiPath 检测到的 ctrlid 格式与其 XSLT 表示中记录的格式不同?

xml - xpath/xslt相等检查多个值

xml - 使用 XSLT 删除 xml 标签

java - 从 XML 文件 java 中读取深层的特定元素

java - 使用 JDBC ResultSet 构建 XML 文档对象

java - 没有名称/前缀的 Jackson QName 序列化

xslt - 如何在键之间共享匹配模式?