You can't do this with XPath (XPath can't create new nodes, it can only select existing nodes). But it is very straightforward to do with either XSLT or XQuery.
 
My SQL is very rusty so I don't know if this is an exact equivalent, but the XQuery statement would look something like this:
 
for $r1 in doc('xmldoc1.xml')/*/RECORD,
    $r2 in doc('xmldoc2.xml')/*/RECORD
where $r1.Number = $r2.ID
return
  <RECORD>{
    $r1/*,
    $r2/(* except ID)
  }</RECORD>
 
Michael Kay
http://www.saxonica.com/


From: Erik Rens [mailto:mail2rens@gmail.com]
Sent: 30 March 2009 15:17
To: Mailing list for the SAXON XSLT and XQuery processor
Subject: Re: [saxon] Fwd: Xpath 2.0 sequences and Saxon examples problems.


Hi there,

I was looking on the Internet if there is a way to combine XML documents with XPath in Java. Ofcourse I can write it by myself but i can imagine there is also an XPath statement that will do that for you (much easier). I coudn't found it yet, but meaby some of you know if there is posibility to do that with XPath in Java.

In this situation I want to join element Number from XMLDOC 1 with element ID 10 from XMLDOC 2. Is there something like on SQL statement to do this.
SELECT doc1.*, doc2.* FROM XMLDOC1 as doc1 INNER JOIN XMLDOC2 as doc2 ON doc1.Number = doc2.ID.


Example:
---------------------XMLDOC1---------------------
   <RECORD>
      <Number>10</Number>
      <Title>Eats, Shoots &amp; Leaves</Title>
      <Author>Lynne Truss</Author>
      <ISBN13>978-1592400874</ISBN13>
      <Pages>209</Pages>
   </RECORD>
<RECORD>
      ..... ..................etc.
</RECORD>

---------------------XMLDOC2---------------------
   <RECORD>
      <ID>10</ID>
      <Name>Miranda</Name>
      <Date>06-04-2006</Date>
      <Age>47</Age>
   </RECORD>
<RECORD>
      ..... ..................etc.
</RECORD>


----COMBINED XMLDOC 1 and 2---------------

RECORD>
      <Number>10</Number>
      <Title>Eats, Shoots &amp; Leaves</Title>
      <Author>Lynne Truss</Author>
      <ISBN13>978-1592400874</ISBN13>
      <Pages>209</Pages>
      <Name>Miranda</Name>
      <Date>06-04-2006</Date>
      <Age>47</Age>
</RECORD>


If somebody know the answer thank you..