根据阈值将 XML 文件拆分为多个文件 [英] Split XML file into multiple files based on a threshold value

查看:35
本文介绍了根据阈值将 XML 文件拆分为多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据预定值拆分以下 XML 文件,在此示例中,假设我想将每个创建的文件中的项目"节点限制为三 (3) 个.

I need to split the following XML file based on a predetermined value, for this example, lets assume I want to limit the "Item" node to three (3) within each file created.

这是一个示例输入 XML 文件:

Here's a sample input XML file:

<Items>
  <Item>
    <Title>Title 1</Title>
    <DueDate>01-02-2008</DueDate>
  </Item>
  <Item>
    <Title>Title 2</Title>
    <DueDate>01-02-2009</DueDate>
  </Item>
  <Item>
    <Title>Title 3</Title>
    <DueDate>01-02-2010</DueDate>
  </Item>
  <Item>
    <Title>Title 4</Title>
    <DueDate>01-02-2011</DueDate>
  </Item>
  <Item>
    <Title>Title 5</Title>
    <DueDate>01-02-2012</DueDate>
  </Item>
  <Item>
    <Title>Title 6</Title>
    <DueDate>01-02-2013</DueDate>
  </Item>
  <Item>
    <Title>Title 7</Title>
    <DueDate>01-02-2013</DueDate>
  </Item>
</Items>

基于阈值 3 的期望输出将是三个文件,其中两个包含 3 个项目",最后一个包含剩余的项目",即一个.

The desired output based on the threshold value of 3, would be three files, two of which contain 3 "Item", and the last one containing the remaining "items", which would be one.

这是我的 XSLT 示例,它允许我为每个项目拆分它们,从而生成七个单独的文件,但是,我希望根据项目"节点的特定限制来限制文件的大小每个文件.

Here's a sample of my XSLT which does allow me to split them for each item, which results into seven separate files, however, what I desire is to limit the size of the file based on a certain limit of "Item" node per file.

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

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

<xsl:template match="/">

 <xsl:for-each select="//Item">
  <xsl:variable name="nTitle" select="Title"/>
  <xsl:variable name="filename" select="concat('Items\',$nTitle,'-','.xml')" />
  <xsl:value-of select="$filename" />
   <xsl:result-document  href="{$filename}"  format="xml">
         <xsl:copy-of select="."/>
  </xsl:result-document>
 </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

推荐答案

此样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pItemsNumber" select="3"/>
    <xsl:template match="Items">
        <xsl:for-each-group select="Item"
                            group-adjacent="(position()-1) idiv $pItemsNumber">
            <xsl:result-document  href="Items\{current-grouping-key()}.xml">
                <Items>
                    <xsl:copy-of select="current-group()"/>
                </Items>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<Items>
    <Item>
        <Title>Title 1</Title>
        <DueDate>01-02-2008</DueDate>
    </Item>
    <Item>
        <Title>Title 2</Title>
        <DueDate>01-02-2009</DueDate>
    </Item>
    <Item>
        <Title>Title 3</Title>
        <DueDate>01-02-2010</DueDate>
    </Item>
</Items>

<?xml version="1.0" encoding="UTF-8"?>
<Items>
    <Item>
        <Title>Title 4</Title>
        <DueDate>01-02-2011</DueDate>
    </Item>
    <Item>
        <Title>Title 5</Title>
        <DueDate>01-02-2012</DueDate>
    </Item>
    <Item>
        <Title>Title 6</Title>
        <DueDate>01-02-2013</DueDate>
    </Item>
</Items>

<?xml version="1.0" encoding="UTF-8"?>
<Items>
    <Item>
        <Title>Title 7</Title>
        <DueDate>01-02-2013</DueDate>
    </Item>
</Items>

糟糕!

这篇关于根据阈值将 XML 文件拆分为多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆