无论大小写,都在XSLT中标记单词

问题描述 投票:0回答:1

无论如何,我都需要标记单词。我找到了这个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中安全的发生并不完全匹配。所以我没有得到我想要的输出:

<?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 xslt-2.0
1个回答
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>&#10;</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>&#10;</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>
© www.soinside.com 2019 - 2024. All rights reserved.