鉴于以下输入xml文件,我需要搜索值,并在输入xml中替换它们
搜索值在xslt中,每行必须替换为其等效行。
<TABLE NAME="TEST">
<DATA RECORDS="78">
<catalog>
<book id="bk109">
<description>2ος Όροφος</description>
</book>
<book id="bk110">
<description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description>
</book>
<book id="bk111">
<description>An anthology of HORROR stories about roaches, centipedes, scorpions and other insects.</description>
</book>
<book id="bk112">
<description>2ος όροφος</description>
</book>
<book id="bk113">
<description>An anthology of horror stories about roaches, centipedes, scorpions and other insects.</description>
</book>
<book id="bk114">
<description>Microsoft's .NET initiative is explored in detail in this deep PROGRAMMER's reference.</description>
</book>
<book id="bk115">
<description>An anthology of HORROR stories about roaches, centipedes, scorpions and other insects.</description>
</book>
<book id="bk116">
<description>An anthology of horror stories about roaches, centipedes, scorpions and other insects. Beware, this must not be matched.</description>
</book>
<book id="bk114">
<description>Microsoft's .NET initiative is explored in detail in this deep PROGRAMMER's reference. Beware, this must not be matched.</description>
</book>
</catalog>
</DATA>
</TABLE>
以及以下xslt:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:functx="http://www.functx.com"
exclude-result-prefixes="xs functx">
<xsl:param name="search-text" as="xs:string">2ος Όροφος
2ος όροφος</xsl:param>
<xsl:param name="replacement-text" as="xs:string">2ος όροφoς
2ος όροφoς</xsl:param>
<xsl:param name="search-terms" as="xs:string*" select="tokenize($search-text, '\r?\n')"/>
<xsl:param name="search-terms-is" as="xs:string*" select="for $term in $search-terms return concat('^', lower-case(functx:escape-for-regex($term)), '$')"/>
<xsl:param name="replace-terms" as="xs:string*" select="tokenize($replacement-text, '\r?\n')"/>
<xsl:include href="http://www.xsltfunctions.com/xsl/functx-1.0-nodoc-2007-01.xsl"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="description[some $search-term in $search-terms-is satisfies matches(., $search-term, 'i')]">
<xsl:copy>
<xsl:variable name="matched-term" as="xs:string" select="$search-terms-is[matches(current(), ., 'i')]"/>
<xsl:variable name="replacement" as="xs:string" select="$replace-terms[index-of($search-terms-is, $matched-term)]"/>
<xsl:value-of
select="$replacement"/>
</xsl:copy>
</xsl:template>
</xsl:transform>
我得到了错误。
如何修改上述内容,以便保留xslt所有代码,但避免出现以下错误?
<?xml version="1.0" encoding="UTF-8"?><TABLE NAME="TEST">
<DATA RECORDS="78">
<catalog>
<book id="bk109">
Error on line 30
XTTE0570: A sequence of more than one item is not allowed as the value of variable
$matched-term ("^2ος όροφος$", "^2ος όροφος$")
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]/catalog[1]/book[1]/description[1]
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]/catalog[1]/book[1]
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]/catalog[1]
at xsl:apply-templates (#23)
processing /TABLE/DATA[1]
in built-in template rule
住在这里: http://xsltransform.net/pNvs5wd
我还要感谢xslt,其中搜索和替换值可以加载到外部文件中。即search.txt,replace.txt
答案 0 :(得分:0)
发布的代码假设任何搜索词都输入一次,并由XSLT转换为<xsl:param name="search-terms-is" as="xs:string*" select="for $term in $search-terms return concat('^', lower-case(functx:escape-for-regex($term)), '$')"/>
中的小写。此外,输入数据与some $search-term in $search-terms-is satisfies matches(., $search-term, 'i')
中的搜索项的匹配是使用i
标志来完成的,以使用不区分大小写的匹配。您的两个术语2ος Όροφος
和2ος όροφος
仅在字符的情况下有所不同,因此它们都会导致输入数据匹配。由于存储匹配的变量类型为xs:string
,只需要一次匹配即可得到该错误。
要解决此问题,您应确保搜索字词仅包含一个特定字词,或者您需要删除使用lower-case
和i
标记。
对于使用带有搜索和替换列表的文本文件,XSLT 2及更高版本支持您可以在
中使用的XPath 2和更高版本的unparsed-text(file-uri, optional-encoding)
函数
<xsl:param name="searchFile" as="xs:string">search-terms.txt</xsl:param>
<xsl:param name="search-text" as="xs:string" select="unparsed-text($searchFile)"/>
从文本文件加载术语。