gpt4 book ai didi

xml - XSLT XML : highlight a search word in search results

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

这是 xml:

<?xml version="1.0" encoding="UTF-8"?>
<file>
<text>
<p>
<sentence>I bought kiwi at the grocery store.</sentence>
<sentence>I also bought bananas at the store.</sentence>
<sentence>Then, I bought a basket at another store.</sentence>
</p>
<p>
<sentence>You bought kiwi at the grocery store.</sentence>
<sentence>You also bought bananas at the store.</sentence>
<sentence>Then, You bought a basket at another store.</sentence>
</p>
</text>
</file>

这是 XSLT:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="sentence">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="file/text/p/sentence[contains(.,$search)]"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

我需要突出显示 $search$search="kiwi" 时,结果中的单词是这样的:

我买了<mark>猕猴桃</mark>在杂货店。

您购买了 <mark>猕猴桃</mark>在杂货店。

请帮忙!

最佳答案

要突出显示所有出现的搜索字符串,请更改:

<xsl:template match="sentence">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>

到:

<xsl:template match="sentence"> 
<p>
<xsl:call-template name="hilite">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="search-string" select="$search"/>
</xsl:call-template>
</p>
</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>

关于xml - XSLT XML : highlight a search word in search results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007167/

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