在给定不区分大小写的搜索值的情况下,用XML替换不区分大小写的值

时间:2017-09-05 13:37:16

标签: xslt-2.0

我面临以下问题:
我有源XML文件:

<TABLE NAME="TEST">
    <DATA RECORDS="78">
    <catalog>
       <book id="bk109">
          <description>An anthology of horror stories about roaches, centipedes, scorpions  and other insects.</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>An anthology of horror stories about roaches, centipedes, scorpions  and other insects.</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>

给出以下xsl:

<?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">An anthology of horror stories about roaches, centipedes, scorpions  and other insects.
Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</xsl:param>

    <xsl:param name="replacement-text" as="xs:string">Value we need to store in the (description) element.
Another value we need to store in the (description) element.</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>

结果还可以:

<?xml version="1.0" encoding="UTF-8"?><TABLE NAME="TEST">
<DATA RECORDS="78">
<catalog>
   <book id="bk109">
      <description>Value we need to store in the (description) element.</description>
   </book>
   <book id="bk110">
      <description>Another value we need to store in the (description) element.</description>
   </book>
   <book id="bk111">
      <description>Value we need to store in the (description) element.</description>
   </book>
   <book id="bk112">
      <description>Value we need to store in the (description) element.</description>
   </book>
   <book id="bk113">
      <description>Value we need to store in the (description) element.</description>
   </book>
   <book id="bk114">
      <description>Another value we need to store in the (description) element.</description>
   </book>
   <book id="bk115">
      <description>Value we need to store in the (description) element.</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>

但如果在搜索字词中,我有两倍相同的字词,在不同情况下,我会收到错误

的xsl:

    <?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">An anthology of horror stories about roaches, centipedes, scorpions  and other insects.
An anthology of HORROR stories about roaches, centipedes, scorpions  and other insects.</xsl:param>

    <xsl:param name="replacement-text" as="xs:string">Value we need to store in the (description) element.
Another value we need to store in the (description) element.</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>

错误:

<?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 ("^an anthology of horror storie...", "^an anthology of horror storie...") 
  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

我怎么能改变样式表,以免发生这种错误? 这个例子是由Martin Honnen在线上传的,他很友好地回答了我之前提出的问题。 在线http://xsltransform.net/gVhD8RA

0 个答案:

没有答案