如果某些元素匹配,则XSLT将一个XML复制到另一个XML



我有以下XML的:

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>

我需要从notifications-source-path.xml复制 <MsgText>到notifications.xml(notifications/notifications/emailnotification/emailnotification/tag主题之后(,如果<NotifId>匹配(。有人可以向我展示如何正确实施此操作的方式吗?我打算将撒克逊人库用于此。

编辑:

所以到现在为止,我已经创建了此代码:

<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>

,它从html_notifications中选择了我。但是我不知道如何比较notifid,然后将所选的msgText应用于target xml。

edit2:输出应为:

<?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>

,但我不知道如何比较Notifid,然后应用 选定的msgText用于target xml。

您正在使用撒克逊人,最新版本的撒克逊人支持XSLT 3.0,XSLT 3.0具有新的指令XSL:合并,该指令是为此要求定制的。您想要这样的东西(修订以考虑有关所需结果的新信息(:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0" expand-text="yes">
    <!--<xsl:variable name="notifications" select="doc('notifications.xml')"/>
        <xsl:variable name="notifications-source-path" select="doc('notifications-source-path.xml')"/>-->
    <xsl:variable name="notifications">
        <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>
    </xsl:variable>
    <xsl:variable name="notifications-source-path">
        <Notifications>
            <Notification>
                <NotifId>1</NotifId>
                <MsgText>
                    <![CDATA[notif 1]]>
                </MsgText>
            </Notification>
            <Notification>
                <NotifId>2</NotifId>
                <MsgText>
                    <![CDATA[notif 2]]>
                </MsgText>
            </Notification>
        </Notifications>
    </xsl:variable>
    <xsl:mode on-no-match="shallow-copy"/>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

    <xsl:template name="xsl:initial-template">
        <Notifications>
            <xsl:copy-of select="$notifications//BatchId"/>
            <xsl:merge>
                <xsl:merge-source name="notifications" select="$notifications/*/Notification">
                    <xsl:merge-key select="NotifId"/>
                </xsl:merge-source>
                <xsl:merge-source name="notifications-source-path"
                    select="$notifications-source-path/*/Notification">
                    <xsl:merge-key select="NotifId"/>
                </xsl:merge-source>
                <xsl:merge-action>
                    <Notification>
                        <NotifId>{(current-merge-group()/NotifId)[1]}</NotifId>
                        <EmailNotification>
                            <xsl:apply-templates select="current-merge-group()/EmailNotification/*, current-merge-group()/MsgText"/>
                        </EmailNotification>
                    </Notification>
                </xsl:merge-action>
            </xsl:merge>
        </Notifications>
    </xsl:template>

</xsl:stylesheet>

此提供了预期的结果,除了(a(丢失的传输时间元素 - 我看不到您从哪里获得的,(b(XSLT无法将CDATA部分从输入中复制到输出 - CDATA - 此处的CDATA无论如何都没有执行任何有用的目的。

如果您想要XSLT 2.0解决方案,则可以使用XSL:for-each组获得非常相似的结果。这是这个版本:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="2.0">
    <!--<xsl:variable name="notifications" select="doc('notifications.xml')"/>
        <xsl:variable name="notifications-source-path" select="doc('notifications-source-path.xml')"/>-->
    <xsl:variable name="notifications">
        <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>
    </xsl:variable>
    <xsl:variable name="notifications-source-path">
        <Notifications>
            <Notification>
                <NotifId>1</NotifId>
                <MsgText>
                    <![CDATA[notif 1]]>
                </MsgText>
            </Notification>
            <Notification>
                <NotifId>2</NotifId>
                <MsgText>
                    <![CDATA[notif 2]]>
                </MsgText>
            </Notification>
        </Notifications>
    </xsl:variable>
    <xsl:template match="*">
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>

    <xsl:template name="main">
        <Notifications>
            <xsl:copy-of select="$notifications//BatchId"/>
            <xsl:for-each-group select="($notifications, $notifications-source-path)/*/Notification" group-by="NotifId">
                    <Notification>
                        <NotifId><xsl:value-of select="current-grouping-key()"/></NotifId>
                        <EmailNotification>
                            <xsl:apply-templates select="current-group()/EmailNotification/*, current-group()/MsgText"/>
                        </EmailNotification>
                    </Notification>
            </xsl:for-each-group>
        </Notifications>
    </xsl:template>

</xsl:stylesheet>