我想在 XSLT 中每三个大写字母后添加零宽度空格。在这里,我想选择文档中的所有文本节点,并在该文本节点中过滤大写单词。
我的 XML 示例代码是:
<doc>
<front>
<lable>this is a TEST TEXT</lable>
<para>This is a TEST TEXT with UPPER and Lower</para>
</front>
<middle>
<lable>this is a TEST TEXT</lable>
<para>This is a TEST TEXT with UPPER and Lower</para>
</middle>
<back>
<lable>This is a TEST TEXT</lable>
<para>This is a TEST TEXT with UPPER and Lower</para>
</back>
</doc>
我写的 XSLT 是:
<xsl:template match="*/text()" priority="100">
<xsl:analyze-string select="." regex="^[A-Z]+">
<xsl:matching-substring>
<xsl:variable name="upperWord" select="substring(.,3)"/>
<xsl:value-of select="concat($upperWord,'​')"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
我期待的输出:
<doc>
<front>
<lable>this is a TES​T TEX​T</lable>
<para>his is a TES​T TEX​T with UPP​ER and Lower</para>
</front>
<front>
<lable>this is a TES​T TEX​T</lable>
<para>his is a TES​T TEX​T with UPP​ER and Lower</para>
</front>
<front>
<lable>this is a TES​T TEX​T</lable>
<para>his is a TES​T TEX​T with UPP​ER and Lower</para>
</front>
</doc>
Output I got:
<doc>
<front>
<lable>this is a TEST TEXT</lable>
<para>his is a TEST TEXT with UPPER and Lower</para>
</front>
<middle>
<lable>this is a TEST TEXT</lable>
<para>his is a TEST TEXT with UPPER and Lower</para>
</middle>
<back>
<lable>his is a TEST TEXT</lable>
<para>his is a TEST TEXT with UPPER and Lower</para>
</back>
</doc>
在这里,我无法理解为什么选择单词的大写首字母和为什么没有选择所有文本节点。有人可以帮助我解决这个问题吗..谢谢。。
尝试替换
regex="^[A-Z]+"
跟
regex="[A-Z]+"
您应该能够通过删除xsl:analyze-string
并仅使用 replace()
来简化它。
注意:在我的示例中,我使用 xsl:character-map
来保留文本中的实体。您可以剥离它,然后插入实际字符。
例。。。(工作版本在这里)
XML 输入
<doc>
<front>
<lable>this is a TEST TEXT</lable>
<para>This is a TEST TEXT with UPPER and Lower</para>
</front>
<middle>
<lable>this is a TEST TEXT</lable>
<para>This is a TEST TEXT with UPPER and Lower</para>
</middle>
<back>
<lable>This is a TEST TEXT</lable>
<para>This is a TEST TEXT with UPPER and Lower</para>
</back>
</doc>
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" use-character-maps="chars"/>
<xsl:strip-space elements="*"/>
<xsl:character-map name="chars">
<xsl:output-character character="​" string="&#x200b;"/>
</xsl:character-map>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" priority="1">
<xsl:value-of select="replace(.,'([A-Z]{3})','$1​')"/>
</xsl:template>
</xsl:stylesheet>
XML 输出
<doc>
<front>
<lable>this is a TES​T TEX​T</lable>
<para>This is a TES​T TEX​T with UPP​ER and Lower</para>
</front>
<middle>
<lable>this is a TES​T TEX​T</lable>
<para>This is a TES​T TEX​T with UPP​ER and Lower</para>
</middle>
<back>
<lable>This is a TES​T TEX​T</lable>
<para>This is a TES​T TEX​T with UPP​ER and Lower</para>
</back>
</doc>