XSLT copy element from one xml to other xml if some element matches

删除回忆录丶 提交于 2019-12-13 09:53:34

问题


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>

回答1:


But I don't know how to compare the NotifId and then apply that selected MsgText to target xml.

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

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

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:

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


来源:https://stackoverflow.com/questions/44762709/xslt-copy-element-from-one-xml-to-other-xml-if-some-element-matches

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!