使用xslt 2.0拆分大型xml文件

时间:2021-08-16 21:32:27

I have this source xml file.

我有这个源xml文件。

   <DATA>
    <DATASET>      
      <KE action="create">
         <A>USVa</A>
         <B>USVb</B>
         <C>USV10</C>             
      </KE>
      <KE>
       ....
      </KE>
    </DATASET>
   </DATA>

The element "KE" exists round about 30000 times. I want to create every 5000 "KE" a new XML file. In the case of 30000 KE-elements must be the result 6 separate xml files and the structure a copy of the source xml.

元素“KE”存在约30000次。我想创建每5000个“KE”一个新的XML文件。在30000 KE-elements的情况下,结果必须是6个单独的xml文件,结构是源xml的副本。

How I can realize this with XSLT 2.0? I'm using saxonhe9-5-1-3j. Many thanks ...

我如何通过XSLT 2.0实现这一目标?我正在使用saxonhe9-5-1-3j。非常感谢 ...

1 个解决方案

#1


6  

Use the XSLT 2.0 functionality xsl:for-each-group and the modulus of the position of KE elements. Then, generate output documents with the xsl:result-document element.

使用XSLT 2.0功能xsl:for-each-group和KE元素的位置模数。然后,使用xsl:result-document元素生成输出文档。

My sample XSLT code creates a new result-document for groups of 3 KE elements. Adjust this number to "5000" for your input XML.

我的示例XSLT代码为3个KE元素组创建了一个新的结果文档。将此数字调整为“5000”以输入XML。

Stylesheet

1 Simplified the stylesheet, thanks to @Martin Honnen. 2 Edited again, suggested by @michael.hor257k.

1感谢@Martin Honnen,简化了样式表。 2再次编辑,@ michael.hor257k建议。

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/DATA">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="DATASET">
  <xsl:for-each-group select="KE" group-starting-with="KE[(position() -1)mod 3 = 0]">
     <xsl:variable name="file" select="concat('ke',position(),'.xml')"/>
     <xsl:result-document href="{$file}">
        <DATA>
           <DATASET>
              <xsl:copy-of select="current-group()"/>
           </DATASET>
        </DATA>
     </xsl:result-document>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

You get the following output (I have numbered the KE elements for convenience, the stylesheet does not rely on the n attribute).

您得到以下输出(为方便起见,我已对KE元素进行编号,样式表不依赖于n属性)。

Output: ke1.xml

<?xml version="1.0" encoding="UTF-8"?>
<DATA>
 <DATASET>
  <KE n="1" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="2" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="3" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
 </DATASET>
</DATA>

Output: ke2.xml

<?xml version="1.0" encoding="UTF-8"?>
<DATA>
 <DATASET>
  <KE n="4" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="5" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="6" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
 </DATASET>
</DATA>

The other output documents look the same.

其他输出文档看起来一样。

#1


6  

Use the XSLT 2.0 functionality xsl:for-each-group and the modulus of the position of KE elements. Then, generate output documents with the xsl:result-document element.

使用XSLT 2.0功能xsl:for-each-group和KE元素的位置模数。然后,使用xsl:result-document元素生成输出文档。

My sample XSLT code creates a new result-document for groups of 3 KE elements. Adjust this number to "5000" for your input XML.

我的示例XSLT代码为3个KE元素组创建了一个新的结果文档。将此数字调整为“5000”以输入XML。

Stylesheet

1 Simplified the stylesheet, thanks to @Martin Honnen. 2 Edited again, suggested by @michael.hor257k.

1感谢@Martin Honnen,简化了样式表。 2再次编辑,@ michael.hor257k建议。

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/DATA">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="DATASET">
  <xsl:for-each-group select="KE" group-starting-with="KE[(position() -1)mod 3 = 0]">
     <xsl:variable name="file" select="concat('ke',position(),'.xml')"/>
     <xsl:result-document href="{$file}">
        <DATA>
           <DATASET>
              <xsl:copy-of select="current-group()"/>
           </DATASET>
        </DATA>
     </xsl:result-document>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

You get the following output (I have numbered the KE elements for convenience, the stylesheet does not rely on the n attribute).

您得到以下输出(为方便起见,我已对KE元素进行编号,样式表不依赖于n属性)。

Output: ke1.xml

<?xml version="1.0" encoding="UTF-8"?>
<DATA>
 <DATASET>
  <KE n="1" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="2" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="3" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
 </DATASET>
</DATA>

Output: ke2.xml

<?xml version="1.0" encoding="UTF-8"?>
<DATA>
 <DATASET>
  <KE n="4" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="5" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
  <KE n="6" action="create">
     <A>USVa</A>
     <B>USVb</B>
     <C>USV10</C>
  </KE>
 </DATASET>
</DATA>

The other output documents look the same.

其他输出文档看起来一样。