gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:32:28 24 4
gpt4 key购买 nike

我想使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com