不分大小写在 XSLT 中标记单词 [英] Tagging words in XSLT regardless of case
本文介绍了不分大小写在 XSLT 中标记单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无论大小写,我都需要标记单词.我发现了这个 很好的工作答案,它可以处理匹配的案例.我进行了一些更改以更好地说明不区分大小写的问题...
I need to tag words regardless of case. I found this nicely working answer which does the job with matched case. I've made some changes to illustrate better the case insensitivity issue...
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 并不完全匹配.所以我没有得到我想要的输出:
But the occurrence of safe in Safeco is not an exact match due to case. So I don't get the output I want:
<?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>
如何在不考虑大小写的情况下找到所有出现的情况并在输出中保留原始大小写?
How can I find all occurrences regardless of case and also retain the original case in the output?
推荐答案
这在 XSLT 2.0 中更容易做到,因为它支持正则表达式:
This is much easier to do in XSLT 2.0 with is support for regular expressions:
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>
这篇关于不分大小写在 XSLT 中标记单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文