我有一個包含以下標記的XML文件合併與整合節點含量值
<xml>
<content relationship="regula">
**<source attribute1="RSC1985s5c1" attribute2="6(17)"/>**
<target attribute1="LRC1985s5c1" attribute1="6(17)1"/>
</content>
<content relationship="translation-of">
**<source attribute1="RSC1985s5c1" attribute2="6(17)"/>**
<target attribute1="LRC1985s5c4" attribute2="6(17)1"/>
</content>
<content relationship="translation-of">
**<source attribute1="RSC1985s5c2" attribute2="7(17)"/>**
<target attribute1="LRC1985s5c2" attribute2="7(17)"/>
</content>
<content relationship="translation-of">
**<source attribute1="RSC1985s5c1" attribute2="6(17)"/>**
<target attribute1="LRC1985s5c6" attribute2="6(17)2"/>
</content>
</xml>
什麼,我要的是節點的內容合併到一個新的節點,如果ATTRIBUTE1和attrbite2值的源節點是相等的。所以輸出應該像
<xml>
<transformed relationship="merged">
<source attribute1="RSC1985s5c1" attribute2="6(17)"/>
<target attribute1="LRC1985s5c1" attribute2="6(17)1"/>
<target attribute1="LRC1985s5c4" attribute2="6(17)1"/>
<target attribute1="LRC1985s5c6" attribute2="6(17)2"/>
</transformed>
<transformed relationship="non-merged">
<source attribute1="RSC1985s5c2" attribute2="7(17)"/>
<target attribute1="LRC1985s5c2" attribute2="7(17)"/>
</transformed>
</xml>
所以第一個兩個節點具有彼此相等源ATTRIBUTE1和attribute2值,這就是爲什麼我有將它們組合起來作爲一個新的節點。源中的第三個節點與其他爲什麼我分開輸出的節點不匹配。我嘗試使用foreach循環,但無法得到適當的解決方法。感謝您的幫助,如果我們可以通過使用模板匹配來實現。
具有子節點「源」的相同屬性的任何內容節點應該組合在一起,而不管它們的位置如何。的關係將得到改變爲合併後的人與非合併項目「合併」這將是「非合併」
@_atif:任何兩個「內容」節點與「共同」兒童合併?是否只有兩個相鄰的「內容」節點要合併?如果兩個以上的「內容」節點具有「共同」元素會怎樣? 「關係」屬性是否應該因合併而丟失?這個問題中有太多事情不清楚。請編輯並提供缺少的信息。 –
任何具有相同屬性的子節點「源」的內容節點都應該組合在一起,無論它們的位置如何。對於合併後的關係將變爲「合併」,非合併後的項目將變爲「未合併」 – atif
@_atif:嗯,在評論中這是很好的說法,但它屬於問題 - 請編輯您的問題並在那裏提供這些信息。此外,你沒有回覆我的問題什麼應該是預期的輸出id多於兩個節點將被合併 - 請在你的問題中提供這樣的例子。我絕對不願意猜測你可能沒有想過的事情...... –