This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML to XML using XSLT or other solution

 
Nitti Lin
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to doulble check if XSLT would resolve my problem before I started...

I have two XML A and B. Both of them are almost the same, the only differences is XML A has element <gender> before <age> while XML B has <age> before <gender>.

Ie:
XML A

<gender>M</gender>
<age>20</age>

XML B
<age>20></age>
<gender>M</gender>

I'm using Jaxb to parse XML A without any problems at all, but if I use the same classes to parse XML B, it failed because parser is expecting <gender> not <age>.

1) Does anyone know if I can use XSLT to transform XML B to be like XML A? This way, I don't need to re-engineer the whole system.

2) Is it possible that I can configur Jaxb to accept the different order of elements?

I need you guys' advises on this. Do I need (or can I use) XSLT to resolve my problem?

Thanks,

Nitti
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1) Does anyone know if I can use XSLT to transform XML B to be like XML A? This way, I don't need to re-engineer the whole system.


Yes, XSLT can be used to reorder the elements as you want.

2) Is it possible that I can configur Jaxb to accept the different order of elements?

Not sure how JAXB works so I can't comment on this part of your post. But if at all possible, I would look into this approach more than using XSLT. I, personally, would look at making JAXB work generically (is that possible?) rather than be order dependent.

Good luck.

- m
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about trying to rely on the ordering of elements in XML. The only "ordering" it can supply is based on what elements may contain others (as per a DTD). In XML:

is identical to:

So the only reliable way round this is to redefine your XML document (perhaps to use attributes rather than elements), change to using a DOM parser or (at a real push) handle it as a String with RegEx.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI.....

Originally posted by Paul Sturrock:
Forget about trying to rely on the ordering of elements in XML. The only "ordering" it can supply is based on what elements may contain others (as per a DTD). In XML:

is identical to:



ummm....don't forget the possibility that using a Schema, you can specify the order, by specifying a sequence, in which you should write child elements. That is possible, right?

So, its not always true that the above two instances are identical.
Use caution depending on what your application does.
Thanks.

- m
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ummm....don't forget the possibility that using a Schema, you can specify the order, by specifying a sequence, in which you should write child elements. That is possible, right?

Ah yes. Good idea.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic