I have following xml's:
notifications-source-path.xml
<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
<Notification>
<NotifId>1</NotifId>
<MsgText>
<![CDATA[notif 1]]>
</MsgText>
</Notification>
<Notification>
<NotifId>2</NotifId>
<MsgText>
<![CDATA[notif 2]]>
</MsgText>
</Notification>
</Notifications>
and notifications.xml
<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
<BatchId>1123213333</BatchId>
<Notification>
<NotifId>1</NotifId>
<EmailNotification>
<SenderAddress>abc@def.ghi</SenderAddress>
<Subject>SBJ2</Subject>
</EmailNotification>
</Notification>
<Notification>
<NotifId>2</NotifId>
<EmailNotification>
<SenderAddress>jkl.mno@pqr</SenderAddress>
<Subject>SBJ2</Subject>
</EmailNotification>
</Notification>
</Notifications>
I need to copy <MsgText>
from notifications-source-path.xml to notifications.xml (Notifications/Notification/EmailNotification/MsgText after tag Subject) if <NotifId>
matches. Could someone please show me the way how to properly implement this? I'm planning to use saxon-he library for this.
edit:
so till now I've created this code:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"
cdata-section-elements="MsgText"/>
<xsl:param name="notifications-source-path" select="'html_notifications.xml'"/>
<xsl:template match="Notifications/Notification">
<xsl:apply-templates select="NotifId"/>
</xsl:template>
<xsl:template match="NotifId">
<xsl:variable name="current.notifId" select="NotifId/text()"/>
<MsgText>
<xsl:copy-of
select="document($notifications-source-path)/Notifications/Notification/NotifId/../MsgText/node()"/>
</MsgText>
</xsl:template>
</xsl:stylesheet>
and it selects me the MsgText from html_notifications. But I don't know how to compare the NotifId and then apply that selected MsgText to target xml.
edit2: output should be:
<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
<BatchId>1123213333</BatchId>
<Notification>
<NotifId>1</NotifId>
<EmailNotification>
<SenderAddress>abc@def.ghi</SenderAddress>
<Subject>SBJ2</Subject>
<MsgText><![CDATA[notif 1]]></MsgText>
<TransferTime>2017-12-31T10:00:99</TransferTime>
</EmailNotification>
</Notification>
<Notification>
<NotifId>2</NotifId>
<EmailNotification>
<SenderAddress>jkl.mno@pqr</SenderAddress>
<Subject>SBJ2</Subject>
<MsgText><![CDATA[notif 2]]></MsgText>
<TransferTime>2017-12-31T10:00:99</TransferTime>
</EmailNotification>
</Notification>
</Notifications>
You're using Saxon-HE, and the latest version of Saxon-HE supports XSLT 3.0, and XSLT 3.0 has a new instruction xsl:merge that is custom-made for this requirement. You want something like this (revised to take into account new information about the required result):
This delivers the expected result except (a) the TransferTime element is missing - I can't see where you get that from, and (b) XSLT can't copy CDATA sections from the input to the output - the CDATA here performs no useful purpose anyway.
If you want an XSLT 2.0 solution you could achieve a very similar result using xsl:for-each-group. Here's this version: