无论大小写,我都需要标记单词。我找到了这个nicely working answer which does the job with matched case。我进行了一些更改以更好地说明不区分大小写的问题...
XML:
<?xml version="1.0" encoding="UTF-8"?>
<file>
<text>
<sentence>The safety of the bank’s safe is insured by Safeco.</sentence>
<sentence>A safe place to shelter during a storm is the cellar.</sentence>
</text>
</file>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<!-- ============= -->
<xsl:template match="sentence">
<xsl:param name="search-term"/>
<xsl:call-template name="hilite">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="search-string" select="$search-term"/>
</xsl:call-template><xsl:text>
</xsl:text>
</xsl:template>
<!-- ============= -->
<xsl:template name="hilite">
<xsl:param name="text"/>
<xsl:param name="search-string"/>
<xsl:choose>
<xsl:when test="contains($text, $search-string)">
<xsl:value-of select="substring-before($text, $search-string)"/>
<mark>
<xsl:value-of select="$search-string"/>
</mark>
<xsl:call-template name="hilite">
<xsl:with-param name="text" select="substring-after($text, $search-string)"/>
<xsl:with-param name="search-string" select="$search-string"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- ============= -->
<xsl:template match="/"><xsl:text>
</xsl:text>
<output><xsl:text>
</xsl:text>
<xsl:apply-templates select="file/text/sentence">
<xsl:with-param name="search-term">safe</xsl:with-param>
</xsl:apply-templates>
</output>
</xsl:template>
<!-- ============= -->
</xsl:stylesheet>
我得到的输出:
<?xml version="1.0" encoding="UTF-8"?>
<output>
The <mark>safe</mark>ty of the bank’s <mark>safe</mark> is insured by Safeco.
A <mark>safe</mark> place to shelter during a storm is the cellar.
</output>
但是,由于大小写的原因,在Safeco中出现safe并不完全匹配。所以我没有得到想要的输出:
<?xml version="1.0" encoding="UTF-8"?>
<output>
The <mark>safe</mark>ty of the bank’s <mark>safe</mark> is insured by <mark>Safe</mark>co.
A <mark>safe</mark> place to shelter during a storm is the cellar.
</output>
如何找到所有不分大小写的事件,并在输出中保留原始大小写?
答案 0 :(得分:1)
通过支持正则表达式,这在XSLT 2.0中更容易实现:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/file">
<output>
<xsl:text> </xsl:text>
<xsl:apply-templates select="text/sentence">
<xsl:with-param name="search-term">safe</xsl:with-param>
</xsl:apply-templates>
</output>
</xsl:template>
<xsl:template match="sentence">
<xsl:param name="search-term"/>
<xsl:analyze-string select="." regex="{$search-term}" flags="i" >
<xsl:matching-substring>
<mark>
<xsl:value-of select="." />
</mark>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>
The <mark>safe</mark>ty of the bank’s <mark>safe</mark> is insured by <mark>Safe</mark>co.
A <mark>safe</mark> place to shelter during a storm is the cellar.
</output>