2015-02-09 92 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