File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes XML to XML using XSLT or other solution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML to XML using XSLT or other solution" Watch "XML to XML using XSLT or other solution" New topic
Author

XML to XML using XSLT or other solution

Nitti Lin
Ranch Hand

Joined: May 15, 2003
Posts: 39
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


SCJP, SCWCD, SCJD, (SCEA, MCSD.net)
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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

Joined: Apr 14, 2004
Posts: 10336


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
 
subject: XML to XML using XSLT or other solution
 
Similar Threads
using xsl:for-each tag in xsl file
Customizing JAXB
XSLT and JAXB
dom4j / default attributes?
Process multiple node occurences using JAXB