2015-02-09 96 views
0

我的源XML: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编辑器:

一,删除所有不必要的父母,根据对他们的孩子属性值。

在我的例子中:全部删除<pricelist>其中<category>是x,y。

我部分解决了这个问题。选择是/company/pricelist[category[@id='90' or @id='89']],现在已被删除,可能removeChild();

II。克隆到<category><subcategory>到所有兄弟<item> s。

我没有线索,但如何开始在这一个。我正在考虑一个循环遍历所有价格表,复制它们的<category><subcategory>,并以某种方式将它们克隆到价格表内的每个兄弟<item>(类别和子类别在每个价格表下都是唯一的)。

步骤II结果:

<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> 
+0

采用这种结构需要两个嵌套循环。 – Mouser 2015-02-09 13:28:38

+3

请将您的JS代码添加到问题中。 – 2015-02-09 13:37:08

+0

将用我最初的JavaScript代码更新问题。 – 2015-02-09 14:14:25

回答

1

正如我所说的:两个循环。第一个选择所有剩余价格表,第二个遍历所有item标签。用克隆的categorysubcategory填充它们。

//DO NOT USE THIS SECTION, THIS IS ONLY TO PARSE THE XML STRING SAMPLE INTO A VALID DOCUMENT 
 

 
     var xmlDoc = document.implementation.createDocument("", "", null); 
 
\t \t xmlDoc.preserveWhiteSpace = false; 
 
\t \t var parser = new DOMParser(); 
 
\t \t 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>

+0

完美的作品。感谢您的帮助。我也会用我最初的JavaScript代码更新这个问题。 – 2015-02-09 14:15:49