Ich muss ein Element in Verarbeitung XML mit Element aus anderen XML ersetzen, wenn einige Parameter übereinstimmen. Grundsätzlich ist notifications.xml eine XML-Datei, in der ich Benachrichtigungen verarbeiten muss und nur MsgText durch MsgText aus notification-source.xml ersetzen/hinzufügen muss, wenn notification-source.xml NotifId gleich NotifId der aktuellen Benachrichtigung ist. Wenn keine Übereinstimmung besteht, bewahren Sie die Benachrichtigung unverändert auf.XSLT kopieren Elemente von xml nach Element übereinstimmen
notifications.xml
<?xml version="1.0" encoding="UTF-8"?>
<Notifications>
<BatchId>DOCATTR.BATCHID</BatchId>
<Notification>
<NotifId>1</NotifId>
<Cid>DOCATTR.CID</Cid>
<EmailNotification>
<CcAddress>DOCATTR.EMAILCC</CcAddress>
<CcName>DOCATTR.EMAILCCNAME</CcName>
<BccAddress>DOCATTR.EMAILBCC</BccAddress>
<SenderAddress>DOCATTR.SENDERADDRESS</SenderAddress>
<SenderName>DOCATTR.SENDERNAME</SenderName>
<MsgText>
</MsgText>
<Expiration>DOCATTR.EXPIRATION</Expiration>
<Priority>DOCATTR.PRIORITYNC</Priority>
</EmailNotification>
</Notification>
</Notifications>
Benachrichtigungen-source.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>
Dies ist, was ich habe versucht, aber es ist das Kopieren msgText woanders
<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="notifications" select="/"/>
<xsl:variable name="notifications-source" select="document('notifications-source.xml')"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<Notifications>
<xsl:for-each select="$notifications//Notification">
<xsl:variable name="currentNotifId" select="./NotifId"/>
<xsl:for-each select="$notifications-source//Notification">
<xsl:variable name="remoteNotifId" select="./NotifId"/>
<xsl:if test="$currentNotifId=$remoteNotifId">
<xsl:copy>
<xsl:copy-of select="./MsgText"/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</Notifications>
</xsl:template>
</xsl:stylesheet>
es wäre schön saxon-he 9.6.0-6 zu verwenden, aber wenn nicht anwendbar, kann es neuer sein.
Also habe ich Vorlage zu diesem geändert:
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="MsgText">
<xsl:variable name="currentNotifId" select="../NotifId/text()"/>
<xsl:choose>
<xsl:when test="$notifications-source/Notifications/Notification/NotifId[text() = $currentNotifId]">
<xsl:copy>
<xsl:copy-of select="$notifications-source/Notifications/Notification/NotifId[text() = $currentNotifId]/../MsgText"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="'test'"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Jetzt bin ich nur die MsgText entsprechen. Aber im Moment bekomme ich immer <MsgText><![CDATA[test]]></MsgText>
, also ich gues Ich bin nicht richtig mit Test (wählen)
Ist diese Frage anders als https://stackoverflow.com/questions/44762709/xslt-copy-element-from-one-xml-to-other-xml-if-some-element-matches oder warum fragen Sie ein neuer? Und Sie sollten uns das gewünschte Ergebnis für die von Ihnen geposteten Eingabebeispiele zeigen, wenn Sie möchten, dass wir verstehen, was Sie erreichen möchten, da Ihr Code dies bisher nicht erreicht hat. –
Da ich die alte Frage nicht löschen kann, frage ich neue mit (ich hoffe) bessere Beschreibung. – bilak