【发布时间】:2013-04-19 03:12:02
【问题描述】:
我正在尝试根据元素 ID 合并 XML 文件的子节点。例如,我有以下 XML:
<category:category.fixedChildProducts>
<category:product repositoryId="PROD-001">
<category:product.id>PROD-001</category:product.id>
<category:product.childSKUs>
<category:footwear-sku repositoryId="SKU-001">
<category:footwear-sku.colorDefault>Black</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Large</category:footwear-sku.sizeDefault>
</category:footwear-sku>
<category:footwear-sku repositoryId="SKU-002">
<category:footwear-sku.colorDefault>Black</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Small</category:footwear-sku.sizeDefault>
</category:footwear-sku>
</category:product.childSKUs>
</category:product>
<category:product repositoryId="PROD-001">
<category:product.id>PROD-001</category:product.id>
<category:product.childSKUs>
<category:footwear-sku repositoryId="SKU-003">
<category:footwear-sku.colorDefault>Red</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Large</category:footwear-sku.sizeDefault>
</category:footwear-sku>
<category:footwear-sku repositoryId="SKU-004">
<category:footwear-sku.colorDefault>Red</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Small</category:footwear-sku.sizeDefault>
</category:footwear-sku>
</category:product.childSKUs>
</category:product>
</category:category.fixedChildProducts>
我需要把它改成这样:
<category:category.fixedChildProducts>
<category:product repositoryId="PROD-001">
<category:product.id>PROD-001</category:product.id>
<category:product.childSKUs>
<category:footwear-sku repositoryId="SKU-001">
<category:footwear-sku.colorDefault>Black</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Large</category:footwear-sku.sizeDefault>
</category:footwear-sku>
<category:footwear-sku repositoryId="SKU-002">
<category:footwear-sku.colorDefault>Black</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Small</category:footwear-sku.sizeDefault>
</category:footwear-sku>
<category:footwear-sku repositoryId="SKU-003">
<category:footwear-sku.colorDefault>Red</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Large</category:footwear-sku.sizeDefault>
</category:footwear-sku>
<category:footwear-sku repositoryId="SKU-004">
<category:footwear-sku.colorDefault>Red</category:footwear-sku.colorDefault>
<category:footwear-sku.sizeDefault>Small</category:footwear-sku.sizeDefault>
</category:footwear-sku>
</category:product.childSKUs>
</category:product>
</category:category.fixedChildProducts>
基本上,我需要遍历产品的repositoryId 属性,并将具有相同产品repositoryId 的所有childSKUs 组合在一起。每个产品节点都应该有一个唯一的repositoryId。请帮忙。
【问题讨论】: