XML : 删除 parent,克隆给兄弟姐妹
XML : Remove parent, clone to siblings
我的来源xml:
<company>
<pricelist>
<category></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
// x number of items
</pricelist>
<pricelist>
<category></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
// x number of items
</pricelist>
// x number of pricelists
</company>
现在,想为给定的 xml 自动构建一个编辑器:
我。根据 children 属性值删除所有不需要的 parents。
在我的示例中:删除所有 <pricelist>
,其中 <category>
是 x,y。
I partially solved this one. The selection is /company/pricelist[category[@id='90' or @id='89']]
that now has to be removed, probably removeChild();
二.将 <category>
和 <subcategory>
克隆到所有同级 <item>
中。
我还不知道如何开始这个。我在想一个遍历所有价目表的循环,复制它们的 <category>
和 <subcategory>
并以某种方式将它们克隆到价目表中的每个同级 <item>
(类别和子类别是唯一的在每个价目表下)。
第二步结果:
<pricelist>
<category>cat1</category>
<subcategory>sub1</subcategory>
<item>
<category>cat1</category>
<subcategory>sub1</subcategory>
</item>
<item>
<category>cat1</category>
<subcategory>sub1</subcategory>
</item>
<item>
<category>cat1</category>
<subcategory>sub1</subcategory>
</item>
...
</pricelist>
正如我所说:两个循环。第一个选择所有剩余的价目表,第二个遍历所有 item
标签。用克隆的 category
和 subcategory
.
填充它们
//DO NOT USE THIS SECTION, THIS IS ONLY TO PARSE THE XML STRING SAMPLE INTO A VALID DOCUMENT
var xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.preserveWhiteSpace = false;
var parser = new DOMParser();
xmlDoc = parser.parseFromString(document.querySelector("textarea").value.trim(),"text/xml");
//END
//first delete the unwanted pricelist
var unwanted = xmlDoc.querySelectorAll("pricelist > category[id='89'], pricelist > category[id='90']");
Array.prototype.map.call(unwanted, function(element){
element.parentNode.parentNode.removeChild(element.parentNode); //elements deleted
});
//now parse the rest
//The first loop will select all pricelists and iterates over them using Array.prototype.map.
var priceLists = xmlDoc.querySelectorAll("pricelist");
Array.prototype.map.call(priceLists, function(element){
//select the category and sub
var cat = element.getElementsByTagName("category")[0];
var subCat = element.getElementsByTagName("subcategory")[0];
//now select all item tags. Iterate over them with a normal loop for more clarity.
var items = element.getElementsByTagName("item");
for (var i = 0; i < items.length; ++i)
{
var temp = cat.cloneNode(true); //clone the category
var temp2 = subCat.cloneNode(true); //clone the subcategory
items[i].appendChild(temp); //append them
items[i].appendChild(temp2);
}
});
// display the XML :: NOT PART OF THE SOLUTION CODE
document.getElementById("display").value = new XMLSerializer().serializeToString(xmlDoc.documentElement);
<textarea>
<company>
<pricelist>
<category>test 1</category>
<subcategory>test 2</subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
<pricelist>
<category>test 3</category>
<subcategory>test 4</subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
<pricelist >
<category id="90"></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
<pricelist >
<category id="89"></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
</company>
</textarea>
<textarea id="display" style="width: 400px; height: 500px;"></textarea>
我的来源xml:
<company>
<pricelist>
<category></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
// x number of items
</pricelist>
<pricelist>
<category></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
// x number of items
</pricelist>
// x number of pricelists
</company>
现在,想为给定的 xml 自动构建一个编辑器:
我。根据 children 属性值删除所有不需要的 parents。
在我的示例中:删除所有 <pricelist>
,其中 <category>
是 x,y。
I partially solved this one. The selection is
/company/pricelist[category[@id='90' or @id='89']]
that now has to be removed, probablyremoveChild();
二.将 <category>
和 <subcategory>
克隆到所有同级 <item>
中。
我还不知道如何开始这个。我在想一个遍历所有价目表的循环,复制它们的 <category>
和 <subcategory>
并以某种方式将它们克隆到价目表中的每个同级 <item>
(类别和子类别是唯一的在每个价目表下)。
第二步结果:
<pricelist>
<category>cat1</category>
<subcategory>sub1</subcategory>
<item>
<category>cat1</category>
<subcategory>sub1</subcategory>
</item>
<item>
<category>cat1</category>
<subcategory>sub1</subcategory>
</item>
<item>
<category>cat1</category>
<subcategory>sub1</subcategory>
</item>
...
</pricelist>
正如我所说:两个循环。第一个选择所有剩余的价目表,第二个遍历所有 item
标签。用克隆的 category
和 subcategory
.
//DO NOT USE THIS SECTION, THIS IS ONLY TO PARSE THE XML STRING SAMPLE INTO A VALID DOCUMENT
var xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.preserveWhiteSpace = false;
var parser = new DOMParser();
xmlDoc = parser.parseFromString(document.querySelector("textarea").value.trim(),"text/xml");
//END
//first delete the unwanted pricelist
var unwanted = xmlDoc.querySelectorAll("pricelist > category[id='89'], pricelist > category[id='90']");
Array.prototype.map.call(unwanted, function(element){
element.parentNode.parentNode.removeChild(element.parentNode); //elements deleted
});
//now parse the rest
//The first loop will select all pricelists and iterates over them using Array.prototype.map.
var priceLists = xmlDoc.querySelectorAll("pricelist");
Array.prototype.map.call(priceLists, function(element){
//select the category and sub
var cat = element.getElementsByTagName("category")[0];
var subCat = element.getElementsByTagName("subcategory")[0];
//now select all item tags. Iterate over them with a normal loop for more clarity.
var items = element.getElementsByTagName("item");
for (var i = 0; i < items.length; ++i)
{
var temp = cat.cloneNode(true); //clone the category
var temp2 = subCat.cloneNode(true); //clone the subcategory
items[i].appendChild(temp); //append them
items[i].appendChild(temp2);
}
});
// display the XML :: NOT PART OF THE SOLUTION CODE
document.getElementById("display").value = new XMLSerializer().serializeToString(xmlDoc.documentElement);
<textarea>
<company>
<pricelist>
<category>test 1</category>
<subcategory>test 2</subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
<pricelist>
<category>test 3</category>
<subcategory>test 4</subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
<pricelist >
<category id="90"></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
<pricelist >
<category id="89"></category>
<subcategory></subcategory>
<item></item>
<item></item>
<item></item>
</pricelist>
</company>
</textarea>
<textarea id="display" style="width: 400px; height: 500px;"></textarea>