使用外部文件和xsl的xml不区分大小写的替换

时间:2017-06-13 11:04:31

标签: xml xslt-2.0

我输入以下xml文件input.xml:

<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="ITEMS.DB">
   <DATA RECORDS="33673">
      <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 labs</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:

ASUS
creative labs
This is a test. Replace (all)

和replace.txt:

GIGABYTE
LOGITECH
REPLACEMENT

我正在寻找一种方法,使用xslt-2.0,对search.txt中找到的每个值进行不区分大小写的替换,并在文件replace.txt中找到相应的替换值,因此结果xml应为:< / p>

<?xml version="1.0" encoding="UTF-8"?>
<TABLE NAME="ITEMS.DB">
   <DATA RECORDS="33673">
      <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="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="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="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>REPLACEMENT</MANUFACTURER>
         <AME>pan</AME>
      </RECORD>
   </DATA>
</TABLE>

替换算法是这样的:无论我们在search.txt的第1行中找到什么,都必须替换为文件replace.txt的第1行中的内容,而xsl命令应该接受文件名作为参数, search.txt和replace.txt,与xml和xsl文件在同一目录中找到。每条记录中只能找到一个MANUFACTURER元素。除了。在替换中,我们可能还有其他特殊字符

1 个答案:

答案 0 :(得分:1)

假设您在文本文件的每一行中都有搜索和替换术语,并且您希望在正则表达式中包含需要转义的字符的术语,我现在已经编写了以下样式表来使用{{3 library:

<?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="'replacement.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:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="MANUFACTURER[$search-terms-is[matches(current(), ., 'i')]]">
        <xsl:copy>
            <xsl:variable name="matched-term" as="xs:string" select="$search-terms-is[matches(current(), ., 'i')]"/>
            <xsl:value-of
                select="replace(., $matched-term, $replace-terms[index-of($search-terms-is, $matched-term)], 'i')"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

该样式表适用于我编辑的输入摘要,我没有收到任何错误,匹配的MANUFACTURER元素的内容也会被替换。