【发布时间】:2017-06-26 15:02:06
【问题描述】:
我有以下 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>
如果<NotifId> 匹配,我需要将<MsgText> 从notifications-source-path.xml 复制到notifications.xml(标签主题后的Notifications/Notification/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。
编辑2: 输出应该是:
<?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>
【问题讨论】:
-
欢迎来到 StackOverflow。请访问help center 并阅读How to Ask。如果您发布到目前为止所做的工作,StackOverflow 效果最好。看起来像“请为我写代码”的问题(即使你不是这样打算的)通常会被否决并被搁置为题外话。如果您展示了您尝试过的方法并解释了哪些方法不起作用(包括堆栈跟踪和/或适当的错误消息),那么您更有可能获得帮助。请阅读Why is “Can someone help me?” not an actual question?