I am trying to merge two XML files using XSLT. I need to match the tradeId
node value which must present in both files, and just copy all the content to file1
.
Any help would be welcome. All similar examples are based on attributes and same XML which does not work for me.
File1.xml
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<tradeId>1</tradeId>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
</trade>
<trade>
<tradeId>2</tradeId>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
</trade>
</S:Body>
</S:Envelope>
File2.xml
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<financialexpectation>
<tradeId>1</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
<financialexpectation>
<tradeId>2</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
<financialexpectation>
<tradeId>3</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</S:Body>
</S:Envelope>
Expected output
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<tradeId>1</tradeId>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
<financialexpectation>
<tradeId>1</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
<trade>
<tradeId>2</tradeId>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
<financialexpectation>
<tradeId>2</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
</S:Body>
</S:Envelope>
If you apply this stylesheet to file1.xml
it will explicitly include the corresponding information from file2.xml
using the document
function. There is no need to declare the namespace S
in the stylesheet as the relevant elements have no namespace. I hope this helps.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="trade">
<xsl:copy>
<xsl:apply-templates/>
<xsl:copy-of select="document('file2.xml')//financialexpectation[tradeId=current()/tradeId]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
output
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<tradeId>1</tradeId>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
<financialexpectation>
<tradeId>1</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
<trade>
<tradeId>2</tradeId>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
<financialexpectation>
<tradeId>2</tradeId>
<stateCode>TBD</stateCode>
<methodCode>TBD</methodCode>
<TypeCode>NONE</TypeCode>
</financialexpectation>
</trade>
</S:Body>
</S:Envelope>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.