如果某些元素匹配,则 XSLT 将元素从一个 xml 复制到另一个 xml [英] XSLT copy element from one xml to other xml if some element matches
问题描述
我有以下 xml:
notifications-source-path.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>
和通知.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>
如果
匹配.有人可以告诉我如何正确实施吗?我打算为此使用 saxon-he 库.
从notifications-source-path.xml 复制到notifications.xml(Notifications/Notification/EmailNotification/MsgText 标签后)
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.
所以到目前为止我已经创建了这个代码:
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>
它从 html_notifications 中选择我的 MsgText.但我不知道如何比较 NotifId,然后将选定的 MsgText 应用到目标 xml.
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.
输出应该是:
<?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 来定位 xml.
But I don't know how to compare the NotifId and then apply that selected MsgText to target xml.
您正在使用 Saxon-HE,并且最新版本的 Saxon-HE 支持 XSLT 3.0,并且 XSLT 3.0 有一个新指令 xsl:merge 是针对此需求定制的.您想要这样的东西(修改以考虑有关所需结果的新信息):
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>
这提供了预期的结果,除了 (a) TransferTime 元素丢失 - 我看不出你从哪里得到它,以及 (b) XSLT 不能将 CDATA 部分从输入复制到输出 - 这里的 CDATA无论如何都没有任何用处.
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.
如果您想要一个 XSLT 2.0 解决方案,您可以使用 xsl:for-each-group 获得非常相似的结果.这是这个版本:
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>
这篇关于如果某些元素匹配,则 XSLT 将元素从一个 xml 复制到另一个 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!