Xsl转换优化



我有两个xml文件:

products.xml

<lists>
<list id="0">
      <group id="53149">
        <product id="87563223"/>
        <product id="25000016"/>
      </group>
      <group id="138939">
        <product id="2400004"/>
        <product id="2400005"/>
      </group>
</list>
<list id="1">
      <group id="34181">
        <product id="2249213"/>
      </group>
      <group id="73892">
        <product id="1306005"/>
        <product id="9300001"/>
      </group>
</list>
</lists>

和valid_products.xml

<ValidProducts>
  <product>
     <ID>1306005</ID>
  </product>
  <product>
     <ID>87563223</ID>
  </product>
</ValidProducts>

我使用xslt和Saxon HE处理器从第一个文件产品中删除,其ID与第二个文件中提供的ID不匹配

结果xml:

<lists>
<list id="0">
      <group id="53149">
        <product id="87563223"/>
      </group>
      <group id="138939">
      </group>
</list>
<list id="1">
      <group id="34181">
      </group>
      <group id="73892">
        <product id="1306005"/>
      </group>
</list>
</lists>

这是我的xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" version="2.0">     
    <xsl:output indent="no"/>
    <xsl:strip-space elements="*"/>
    <xsl:param name="f1" />
    <xsl:variable name="doc1" select="document($f1)"/>
    <xsl:variable name="valids" select="$doc1/ValidProducts/product/ID/text()" />
    <xsl:template match="@* | node()"> 
        <xsl:copy> 
            <xsl:apply-templates select="@* | node()"/> 
        </xsl:copy> 
    </xsl:template>  
    <xsl:template match="/lists/list//product[@id[not(. = $valids)]]"/>
</xsl:stylesheet>

我将第二个文件作为参数传递给xsl样式表,它运行良好,但对于大文件(超过200mb),它真的很慢,我该如何优化它?

使用密钥:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" version="2.0">     
    <xsl:output indent="no"/>
    <xsl:strip-space elements="*"/>
    <xsl:param name="f1" />
    <xsl:variable name="doc1" select="document($f1)"/>
    <xsl:key name="by-id" match="ValidProducts/product" use="ID"/>

    <xsl:template match="@* | node()"> 
        <xsl:copy> 
            <xsl:apply-templates select="@* | node()"/> 
        </xsl:copy> 
    </xsl:template>  
    <xsl:template match="/lists/list//product[not(key('by-id', @id, $doc1))]"/>
</xsl:stylesheet>

最新更新