如果我對XML的輸入文件:如何簡化兄弟間的XML節點以確保使用XSLT不存在重複的節點?
<root>
<node id="N1">
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Orange</color>
<year>2000</year>
</attribute>
</orange>
</fruit>
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Orange</color>
<condition>good</condition>
</attribute>
</orange>
</fruit>
</node>
</root>
,這裏是預期輸出:
<root>
<node id="N1">
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Orange</color>
<year>2000</year>
<condition>good</condition>
</attribute>
</orange>
</fruit>
<fruit id="1">
</fruit>
</node>
</root>
如何將兩個兄弟之間的簡化:
- 檢查如果家長相同(水果ID = 1)
- 檢查節點ID和動作是否相同(橙色ID = X動作=創建)
- 如果子元素先前已定義且值相同(橙色),我們將其刪除。
- 如果第二個兄弟的子元素沒有被明確定義,我們將第二個節點添加到第一個節點。 (條件良好)
- 如果節點先前已經被定義,但是具有不同的值(比如顏色爲紅色),我們將節點保持原樣。
另一種情形: 輸入2:
<root>
<node id="N1">
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Orange</color>
</attribute>
</orange>
</fruit>
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Red</color>
<condition>good</condition>
</attribute>
</orange>
</fruit>
</node>
</root>
預期輸出中:
<root>
<node id="N1">
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Orange</color>
<condition>good</condition>
</attribute>
</orange>
</fruit>
<fruit id="1">
<orange id="x" action="create">
<attribute>
<color>Red</color>
</attribute>
</orange>
</fruit>
</node>
</root>
另一種情形:
<root>
<nodeA id="A">
<fruit id="1">
<orange id="x" action="delete" /> <!-- no attributes here -->
</fruit>
<fruit id="1">
<orange id="x" action="delete"/>
<orange id="y" action="delete" />
</fruit>
</nodeA>
</root>
預期輸出:
<root>
<nodeA id="A">
<fruit id="1">
<orange id="x" action="delete" />
</fruit>
<fruit id="1">
<orange id="y" action="delete" />
</fruit>
</nodeA>
</root>
我希望例子給出清晰的想法,請幫助我與轉換文件。 謝謝。
約翰
到目前爲止,你有什麼XSL? – Torious
我想這是這一個的下一章:http://stackoverflow.com/questions/10368853/merge-two-or-more-xml-node-using-xslt。實際上,如果他認爲他需要通過id匹配並構建子節點的超集,我實際上在評論中詢問了@John對Dimitre的回答。看起來像他,事實上,需要它:) –
@John,我得到第一個例子,但我掙扎着第二個。你能否詳細說一下爲什麼'condition'移動到'create orange'的第一個聲明?如果將它合併爲「紅色」並且「條件良好」並且只有一次,那麼基本上,「最近」值最多的超集優先於先前定義的集合。我錯過了什麼嗎? –