Is it possible to join two xml files based on a node value like SQL?
I have two xml files:
<MailPackage>
<Mail>
<id>1</id>
<field_1>foo</field_1>
...
<field_n>bar</field_n>
</Mail>
<Mail>
<id>2</id>
<field_1>... </field_1>
...
</Mail>
....
</MailPackackage>
and
<Transaction_data>
<Transaction>
<id>1</id>
<account_number>10 </account_number>
....
</Transaction>
<Transaction>
<id>1</id>
<account_number> 50 </account_number>
....
</Transaction>
<Transaction>
<id>2</id>
<account_number> 20 </account_number>
....
</Transaction>
</Transaction_data>
Now I'd like to join the two xml files by the value of the 'id' node. The expected result is:
<MailPackage>
<Mail>
<id>1 </id>
<field_1>foo </field_1>
...
<field_n>bar </field_n>
<Transaction_data>
<Transaction>
<Account_number>10</Account_number>
...
</Transaction>
<Transaction>
<Account_number>50 </Account_number>
...
</Transaction>
</Transaction_data>
</Mail>
<Mail>
<id> 2 </id>
<Field_1> ...</Field_1>
...
<Transactions>
<Transaction>
<Account_number> 20 </Account_number>
....
</Transaction>
</Transactions>
</Mail>
</MailPackage>
Can you guys give some help, how to begin?
You can define an
<xsl:key>
to group theTransaction
elements by ID, then insert them at the appropriate places in the main file. This article explains a trick using<xsl:for-each>
to select nodes matching a key from a secondary document - if you have XSLT 2.0 you don't need this trick, just use the three-argument form of thekey()
function.You would process the
<MailPackage>
document as the main input document, and the stylesheet references the transactions document internally.This all assumes that your
Mail
elements all have unique IDs.