2017-06-27 3 views
0

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)

+0

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

+0

Da ich die alte Frage nicht löschen kann, frage ich neue mit (ich hoffe) bessere Beschreibung. – bilak

Antwort

0

Der Pfad <xsl:variable name="currentNotifId" select="../NotifId/text()"/> ist nicht richtig, sollte <xsl:variable name="currentNotifId" select="../../NotifId/text()"/> sein.

Vielleicht möchten Sie lernen, einen Schlüssel stattdessen zu verwenden und einfach den Zustand in ein Übereinstimmungsmuster setzen:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:param name="notifications-source"> 
<Notifications> 
    <Notification> 
     <NotifId>1</NotifId> 
     <MsgText> 
      <![CDATA[notif 1]]> 
     </MsgText> 
    </Notification> 
    <Notification> 
     <NotifId>2</NotifId> 
     <MsgText> 
      <![CDATA[notif 2]]> 
     </MsgText> 
    </Notification> 
</Notifications>   
    </xsl:param> 

    <xsl:key name="note-ref" match="Notification/MsgText" use="../NotifId"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="MsgText[key('note-ref', ../../NotifId, $notifications-source)]"> 
     <xsl:copy-of select="key('note-ref', ../../NotifId, $notifications-source)"/> 
    </xsl:template> 

</xsl:transform> 

Online bei http://xsltransform.net/93dEHFX.

+0

Danke, ich habe es meinen Weg (mit festen Pfad) ... wenn ich Ihre Vorlage in Intellij IDE ausführen, bekomme ich folgenden Fehler: '[FEHLER]: konnte Stylesheet nicht kompilieren [FATAL]: Fehler beim Überprüfen der Art von der Ausdruck 'funcall (Schlüssel, [literal-expr (note-ref), ParentLocationPath (ParentLocationPath (step ("Eltern", -1), step ("Eltern", -1)), step ("Kind", 16)), Parameter-ref (notifications-source/reference)]) '. ' – bilak

+0

Diese Fehlermeldung deutet sehr darauf hin, dass Sie versuchen, diesen XSLT 2.0-Code mit einem XSLT 1.0-Prozessor zu verwenden, obwohl Sie uns gesagt haben, dass Sie Saxon 9 verwenden. –

+0

Ich benutze Saxon 2 in Java ... aber ich habe einige grundlegende Funktionen in IDE getestet. Danke, dass du darauf hingewiesen hast. – bilak

Verwandte Themen