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

时间:2017-06-26 15:02:07

标签: xslt saxon

我遵循xml' s:

通知 - 源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>

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

如果<MsgText>匹配,我需要将来自notifications-source-path.xml的<NotifId>复制到notifications.xml(标记主题后的通知/通知/ EmailNotification / MsgText)。有人可以告诉我如何正确实施这个方法吗?我打算为此使用saxon-he库。

编辑:

所以到目前为止我已经创建了这段代码:

<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中选择了MsgText。但我不知道如何比较NotifId,然后将选定的MsgText应用于目标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>

1 个答案:

答案 0 :(得分:1)

  

但我不知道如何比较NotifId然后应用它   选择MsgText来定位xml。

您正在使用Saxon-HE,并且最新版本的Saxon-HE支持XSLT 3.0,而XSLT 3.0具有针对此要求定制的新指令xsl:merge。你想要这样的东西(修改后考虑了有关所需结果的新信息):

<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)缺少TransferTime元素 - 我无法看到你从哪里得到它,以及(b)XSLT无法将CDATA部分从输入复制到输出 - 无论如何,CDATA在这里没有任何用处。

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

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