2017-06-14 65 views
0
<?xml version="1.0" encoding="UTF-8"?> 
<TABLE NAME="less.db"> 
    <DATA RECORDS="233673"> 
     <RECORD ID="1"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>ASUS</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="1"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>asus</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="14"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>Creative Labs</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="1"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>Creative</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="14"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>This is a test. Replace (all)</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
    </DATA> 
</TABLE> 

search.txt:XML搜索和替換XSL錯誤

ASUS 
creative 
This is a test. Replace (all) 

replace.txt

GIGABYTE 
LOGITECH 
REPLACEMENT 

和XSL:

<?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" 
    xmlns:functx="http://www.functx.com" 
    exclude-result-prefixes="xs functx" 
    version="2.0"> 

    <xsl:param name="search-file" as="xs:string" select="'search.txt'"/> 
    <xsl:param name="replacement-file" as="xs:string" select="'replace.txt'"/> 


    <xsl:param name="search-terms" as="xs:string*" select="tokenize(unparsed-text($search-file), '\r?\n')"/> 

    <xsl:param name="search-terms-is" as="xs:string*" select="for $term in $search-terms return lower-case(functx:escape-for-regex($term))"/> 

    <xsl:param name="replace-terms" as="xs:string*" select="tokenize(unparsed-text($replacement-file), '\r?\n')"/> 

    <xsl:include href="http://www.xsltfunctions.com/xsl/functx-1.0-nodoc-2007-01.xsl"/> 

    <xsl:function name="functx:replace-multi" as="xs:string?" 
     xmlns:functx="http://www.functx.com"> 
     <xsl:param name="arg" as="xs:string?"/> 
     <xsl:param name="changeFrom" as="xs:string*"/> 
     <xsl:param name="changeTo" as="xs:string*"/> 
     <xsl:param name="flags" as="xs:string"/> 

     <xsl:sequence select=" 
      if (count($changeFrom) > 0) 
      then functx:replace-multi(
      replace($arg, $changeFrom[1], 
      functx:if-absent($changeTo[1],''), $flags), 
      $changeFrom[position() > 1], 
      $changeTo[position() > 1]) 
      else $arg 
      "/> 

    </xsl:function> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="MANUFACTURER[some $search-term in $search-terms-is satisfies matches(current(), $search-term, 'i')]"> 
     <xsl:copy> 
      <xsl:variable name="matched-terms" as="xs:string*" select="$search-terms-is[matches(current(), ., 'i')]"/> 
      <xsl:variable name="replacements" as="xs:string*" select="for $t in $matched-terms return $replace-terms[position() = index-of($search-terms-is, $t)]"/> 
      <xsl:value-of 
       select="functx:replace-multi(., $matched-terms, $replacements, 'i')"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

給作爲輸出,以下:

<?xml version="1.0" encoding="UTF-8"?> 
<TABLE NAME="less.db"> 
    <DATA RECORDS="233673"> 
     <RECORD ID="1"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>GIGABYTE</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="1"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>GIGABYTE</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="14"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>LOGITECH Labs</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="1"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>LOGITECH</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
     <RECORD ID="14"> 
     <ID>1</ID> 
     <ROW>0</ROW> 
     <DATE>19/9/2003 12:31:54 μμ</DATE> 
     <al>29/6/2005 10:46:42 πμ</al> 
     <KIT>46123</KIT> 
     <KAP>08</KAP> 
     <YTE>A.IV.C.54</YTE> 
     <HTE>0</HTE> 
     <HEN>0</HEN> 
     <SUM>0</SUM> 
     <LYW>0</LYW> 
     <AMF>29</AMF> 
     <MANUFACTURER>REPLACEMENT</MANUFACTURER> 
     <AME>pan</AME> 
     </RECORD> 
    </DATA> 
</TABLE> 

應該如何製作使用過的xsl,以便進行精確的搜索和替換?例如,LOGITECH實驗室是一個不受歡迎的替代品。我們只想替換每個具有廣告詞(不區分大小寫)的元素,而不是這個詞,無論它在MANUFACTURER元素中的哪個位置都可以找到

+1

不要指望我們下載並打開zip文件,試圖找出導致問題的數據,然後顯示相關樣本在你的問題中的片段。我懷疑你的元素包含幾個搜索詞,你需要在這種情況下解釋你想做什麼,是否需要全部替換它們。 –

+0

我無法識別導致問題的數據...答案是肯定的,全部替換它們,如果有包含多個搜索詞的元素 –

回答

0

我認爲錯誤可能是由包含多個元素搜索詞,這裏是一個適應代碼示例,希望不給這個錯誤:

<?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" 
    xmlns:functx="http://www.functx.com" 
    exclude-result-prefixes="xs functx" 
    version="2.0"> 

    <xsl:param name="search-file" as="xs:string" select="'search.txt'"/> 
    <xsl:param name="replacement-file" as="xs:string" select="'replace.txt'"/> 


    <xsl:param name="search-terms" as="xs:string*" select="tokenize(unparsed-text($search-file), '\r?\n')"/> 

    <xsl:param name="search-terms-is" as="xs:string*" select="for $term in $search-terms return lower-case(functx:escape-for-regex($term))"/> 

    <xsl:param name="replace-terms" as="xs:string*" select="tokenize(unparsed-text($replacement-file), '\r?\n')"/> 

    <xsl:include href="http://www.xsltfunctions.com/xsl/functx-1.0-nodoc-2007-01.xsl"/> 

    <xsl:function name="functx:replace-multi" as="xs:string?" 
     xmlns:functx="http://www.functx.com"> 
     <xsl:param name="arg" as="xs:string?"/> 
     <xsl:param name="changeFrom" as="xs:string*"/> 
     <xsl:param name="changeTo" as="xs:string*"/> 
     <xsl:param name="flags" as="xs:string"/> 

     <xsl:sequence select=" 
      if (count($changeFrom) > 0) 
      then functx:replace-multi(
      replace($arg, $changeFrom[1], 
      functx:if-absent($changeTo[1],''), $flags), 
      $changeFrom[position() > 1], 
      $changeTo[position() > 1]) 
      else $arg 
      "/> 

    </xsl:function> 

    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="MANUFACTURER[some $search-term in $search-terms-is satisfies matches(current(), $search-term, 'i')]"> 
     <xsl:copy> 
      <xsl:variable name="matched-terms" as="xs:string*" select="$search-terms-is[matches(current(), ., 'i')]"/> 
      <xsl:variable name="replacements" as="xs:string*" 
       select="for $t in $matched-terms return $replace-terms[index-of($search-terms-is, $t)]"/> 
      <xsl:value-of 
       select="functx:replace-multi(., $matched-terms, $replacements, 'i')"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

仍然有錯誤,將它插入編輯的問題中,以防萬一幫助,而不知道記錄18有什麼問題。看起來沒問題。 –

+0

編輯問題向我們展示'/ TABLE/DATA [1]/RECORD [18]/MANUFACTURER [1]' –

+0

的數據您可能想嘗試是否將'爲'解決了這個問題。 –