我有想要拆分的大型 XML 文件(“ONIX”标准)。基本结构为:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ONIXmessage SYSTEM "http://www.editeur.org/onix/2.1/short/onix-international.dtd">
<!-- DOCTYPE is not always present and might look differently -->
<ONIXmessage> <!-- sometimes with an attribute -->
<header>
...
</header> <!-- up to this line every out-file should be identical to source -->
<product> ... </product>
<product> ... </product>
...
<product> ... </product>
<ONIXmessage>
我想要做的是将此文件拆分为 n 个大小大致相同的较小文件。为此,我会计算
<product>
的数量节点,将它们除以 n 并将它们克隆到 n 个新的 xml 文件中。我搜索了很多,这个任务似乎比我想象的要难。<header>
的新 XML 文档。节点,但没有 <product>s
.我可以使用正则表达式来做到这一点,但我宁愿使用 xml 工具。 <product>
的最聪明方法是什么?节点到一个新的 XML 文档?对象符号,如 $xml.ONIXmessage.product | % { copy... }
, XPath()
查询(你能用 XPath() 选择 n 个节点吗?)和 CloneNode()
或 XMLReader
/XMLWriter
? 我将非常感谢在正确方向上的一些插入!
最佳答案
一种方法是:
例子:
param($path, [int]$maxitems)
$file = Get-ChildItem $path
################
#Read file
$xml = [xml](Get-Content -Path $file.FullName | Out-String)
$product = $xml.SelectSingleNode("//product")
$parent = $product.ParentNode
#Create copy-template
$copyxml = [xml]$xml.OuterXml
$copyproduct = $copyxml.SelectSingleNode("//product")
$copyparent = $copyproduct.ParentNode
#Remove all but one product (to know where to insert new ones)
$copyparent.SelectNodes("product") | Where-Object { $_ -ne $copyproduct } | ForEach-Object { $copyparent.RemoveChild($_) } > $null
$allproducts = @($parent.SelectNodes("product"))
$totalproducts = $allproducts.Count
$fileid = 1
$i = 0
foreach ($p in $allproducts) {
#IF beggining or full file, create new file
if($i % $maxitems -eq 0) {
#Create copy of file
$newFile = [xml]($copyxml.OuterXml)
#Get parentnode
$newparent = $newFile.SelectSingleNode("//product").ParentNode
#Remove all products
$newparent.SelectNodes("product") | ForEach-Object { $newparent.RemoveChild($_) } > $null
}
#Copy productnode
$cur = $newFile.ImportNode($p,$true)
$newparent.AppendChild($cur) > $null
#Add 1 to "items moved"
$i++
#IF Full file, save
if(($i % $maxitems -eq 0) -or ($i -eq $totalproducts)) {
$newfilename = $file.FullName.Replace($file.Extension,"$fileid$($file.Extension)")
$newFile.Save($newfilename)
$fileid++
}
}
更新:由于性能在这里很重要,我创建了一个新版本的脚本,它使用 foreach 循环和 xml 模板来删除 99% 的读取操作和删除操作的副本。概念仍然相同,但以不同的方式执行。
基准:
10 items, 3 per xml OLD solution: 0.0448831 seconds
10 items, 3 per xml NEW solution: 0.0138742 seconds
16001 items, 1000 per xml items OLD solution: 73.1934346 seconds
16001 items, 1000 per xml items NEW solution: 5.337443 seconds
关于xml - 如何使用 Powershell 将 XML 文件拆分为较小的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058864/