Is there any way to reorder elements in a Node objects such that they match an xsd:sequence
order? I\'ve read through the docs for Node.normalize
, but
If I read your question correctly, what you want is rather a "magical" (as in a simple) way of getting nodes re-arranged according to an external XSD, as opposed to a way to move nodes around using some XML API.
If true, then I am not aware of any API that provides a generic way to achieve such a thing. It is also not a trivial piece of programming, given the complexity of the content models XSD is able to describe.
It is also, under very simple circumstances, virtually impossible to solve. Think of this very simple example: imagine a content model made up of two choices, i) sequence a - b - c , ii) sequence b - c - a. The XML content comes in the order c - b - a (as per your requirement, you want to take in invalid XML, and make it valid). In this example, which is the right way to do it?
Of course, I am not saying this is your scenario. I am simply trying to point out why no one, as far as I know, has come up with a solution to such a problem.
If your content model under the sequence is rather trivial, thus allowing for an unambiguous way of solving it irrespective of the sequence in which your XML arrives, or if the XML content comes in a known, but wrong way, all the time, then you have to code it using your preferred approach to XML manipulation - but I would suggest, don't try to make it generic, it is just going down the rabbit hole ;)....