The moose likes XML and Related Technologies and the fly likes XSLT merging help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSLT merging help" Watch "XSLT merging help" New topic

XSLT merging help

vez yao

Joined: Apr 26, 2011
Posts: 1
Hello, i'm fairly new to xsl stylesheets but have always wanted to know how to merge two xml documents and transform them to output a xml document referencing certain elements.
I've written up two simple xml documents to help you guys understand what i am after.



And the output that i'd like:

So basically the books.xml 'author' element is a foreign key that references the authors.xml author entries. (i'm assuming using muenchian?)
- For each author, "name", "born", "nationality" and "about_me" along with all their "books".
- The books are grouped by "genre" and then sorted on "published_date".
- For each book, the "title", "description, "price" will be stored also.


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The document() function allows you to reference a second XML document from XSLT.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 632
[0] I can show you how to do that, but before doing that, I do not target the output document in exactly the same as what you propose, in particular, I will not put the genre into the attribute to books (as the same author can write different genre of books). Instead I would show the demo with genre put in the book instead.

[0.1] There are quite a few typo and/or consistency in tagging in the sample. I only take the spirit of it and use a consistently named sample (like date vs published_date).

[0.2] Apart from that, the techniques being used to achieve the result are sufficiently varying so that any change of mind can easily be done by yourself with the techniques as demonstrated.

[1] Here is the xsl document. It is supposed to operate on authors.xml and that the books.xml is in the same directory as authors.xml.

[1.1] I suppose there always exist genre and published_date. In case one or both might be absent, it is easy to add a node test with xsl:if to filter the cases.
I agree. Here's the link:
subject: XSLT merging help
It's not a secret anymore!