我正在尝试根据元素 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
属性并将所有childSKUs
具有相同产品的属性组合在一起repositoryId
。每个产品节点都应该有一个唯一的repositoryId
. 请帮忙。