If I have this input file in xml:
<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>
and here is the expected output:
<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>
How to simplify between two sibling:
- check if the parent is the same (fruit id=1)
- check if the node id and action is the same (orange id=x action=create)
- if the child element is already defined previously and the value is the same (color-orange) , we remove it.
- If the child element of the second sibling is not defined perviously we add that second node to the first node. (condition-good)
- If the node is already defined previously but different value (say color-red), we leave the node as it is.
Another scenario:
input2:
<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>
Expected ouput:
<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>
Another scenario:
<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>
Expected output:
<root>
<nodeA id="A">
<fruit id="1">
<orange id="x" action="delete" />
</fruit>
<fruit id="1">
<orange id="y" action="delete" />
</fruit>
</nodeA>
</root>
I hope the example give the clear idea and please help me with the transformation file.
Thanks.
John
John, here is a version that works. It’s somewhat brutal and procedural and so I wonder if you really want to do this kind of logic in XSLT. Here you go:
The following stylesheet:
produces the following result:
All unique attributes are pulled up into the first occurrence of
createaction, only those that have attributes of different values stay infollowing::nodes. If the node has nothing new to add it’s left behind. Here’s how I figure out if the attribute is worth keeping for subsequent occurrences. If the attribute has not been seen before then it’s been already pulled up into the first occurrence so we skip it. If it’s been seen before (= it’s in the collection of same-name attributes on theprecedingaxes) and has a different text value then and only then we keep it.The selectors for what you want to do are getting progressively more complex so I had to use the temporary variables to basically let the templates take a shot at it and then examine if there’s any result to then decide if it’s worth copying into the result tree. There may be a way to convert this logic into match predicates but I am not sure it would be more readable. I hope it makes sense.
UPDATE I updated the solution to also work for your no-attributes corner case. I basically had to silent the no-
attributenodes that are repetitive and also make the main template a little more specific to only work on nodes withattributes. The no-attribute nodes that would “repeat” a with-attribute node will be silent using the main attributes merge logic. The no-attributes nodes that need to stay will be copied over using the default identity transform.