Split large XML to smaller chunks by node count using XSLT

别等时光非礼了梦想. 提交于 2021-01-29 09:01:56

问题


I have a requirement where we are getting a large XML file and I need to transform on small chunks

below is the XML sample with 4 records, I have to transform the XML so I am able to group them in chunks of 2.

<!-- Original XML-->
<EmpDetails>
    <Records>
        <EmpID>1</EmpID>
        <Age>20</Age>
    </Records>
    <Records>
        <EmpID>2</EmpID>
        <Age>21</Age>
    </Records>
    <Records>
        <EmpID>3</EmpID>
        <Age>22</Age>
    </Records>
    <Records>
        <EmpID>4</EmpID>
        <Age>23</Age>
    </Records>
</EmpDetails>


<!-- Expected XML-->
<EmpDetails>
    <Split>
        <Records>
            <EmpID>1</EmpID>
            <Age>20</Age>
        </Records>
        <Records>
            <EmpID>2</EmpID>
            <Age>21</Age>
        </Records>
    </Split>
    <Split>
        <Records>
            <EmpID>3</EmpID>
            <Age>22</Age>
        </Records>
        <Records>
            <EmpID>4</EmpID>
            <Age>23</Age>
        </Records>
    </Split>
</EmpDetails>

I tried few things including below without success.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <EmpDetails>
      <xsl:for-each select="/EmpDetails/Records">
        <Split>
          <Records>
            <EmpID>
              <xsl:value-of select="EmpID"/>
            </EmpID>
            <Age>
              <xsl:value-of select="Age"/>
            </Age>
          </Records>
        </Split>
      </xsl:for-each>
    </EmpDetails>
  </xsl:template>
</xsl:stylesheet>

Thanks Yatan


回答1:


group them in chunks of 2.

This could be done simply by:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/EmpDetails">
    <xsl:copy>
        <xsl:for-each select="Records[position() mod 2 = 1]">
            <Split>
                <xsl:copy-of select=". | following-sibling::Records[1]"/>
            </Split>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Added:

To divide the records into groups of 200, you can do:

    ...
        <xsl:for-each select="Records[position() mod 200 = 1]">
            <Split>
                <xsl:copy-of select=". | following-sibling::Records[position() &lt; 200]"/>
            </Split>
        </xsl:for-each>
    ...

In XSLT 2.0 you could do:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/EmpDetails">
    <xsl:copy>
        <xsl:for-each-group select="Records" group-adjacent="(position() - 1) idiv 200">
            <Split>
                <xsl:copy-of select="current-group()"/>
            </Split>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>


来源:https://stackoverflow.com/questions/65605620/split-large-xml-to-smaller-chunks-by-node-count-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!