Mit XSLT zwei Knoten und Emit XML enthält Attribut vergleichen angibt, ob Wert wurde aktualisiert, hinzugefügt oder gelöscht
Frage
Sie sind bereits Programmierung Frontend-Sprachen für eine lange Zeit und ich selten Begegnung XSLT
an einem Projekt. Nun, hier ist es ... Wir haben derzeit einige Funktionen in unserer XSLT-Datei, die Knoten zu vergleichen und Emittieren XML, die so etwas wie previousValue="Old value"
enthält. Diese Funktion hilft unseren Benutzern zu verstehen, was sich verändert, wenn das Formular angezeigt wird.
in der XML-Suche (siehe unten), ich brauche <ns1:OtherEducationTypeDesc>
und emit XML vergleichen richtig, dass die Staaten, was der alte Wert war.
Ich muß es sieht so etwas wie:
<EducationTypes>
<EducationType Code="11">Engineering</EducationType>
<EducationType Code="12" Value="New Value" PrevValue="Old Value">Other</EducationType>
</EducationTypes>
Ich habe versucht, so viele Informationen wie möglich zu geben, aber wenn Sie etwas anderes benötigen, lassen Sie es mich wissen! Jede Hilfe ist willkommen !! Dank !!
XSLT
<EducationTypes xmlns="omitted">
<xsl:choose>
<xsl:when test="$has-updates">
<!--Get unchanged nodes-->
<xsl:variable name="unchanged-nodes">
<xsl:call-template name="intersection">
<xsl:with-param name="nodes1" select="$educationType-nodes[1]/ns1:Code"/>
<xsl:with-param name="nodes2" select="$educationType-nodes[last()]/ns1:Code"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="education-codes">
<xsl:with-param name="node-set" select="msxsl:node-set($unchanged-nodes)/ns1:Code"/>
<xsl:with-param name="otherText" select="$educationType-nodes[last()]/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
</xsl:call-template>
<!--Get added nodes-->
<xsl:variable name="added-nodes">
<xsl:call-template name="difference">
<xsl:with-param name="nodes1" select="$educationType-nodes[last()]/ns1:Code"/>
<xsl:with-param name="nodes2" select="$educationType-nodes[1]/ns1:Code"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="education-codes">
<xsl:with-param name="node-set" select="msxsl:node-set($added-nodes)/ns1:Code"/>
<xsl:with-param name="otherText" select="$educationType-nodes[last()]/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
<xsl:with-param name="status" select="'added'"/>
</xsl:call-template>
<!--Get deleted nodes-->
<xsl:variable name="deleted-nodes">
<xsl:call-template name="difference">
<xsl:with-param name="nodes1" select="$educationType-nodes[1]/ns1:Code"/>
<xsl:with-param name="nodes2" select="$educationType-nodes[last()]/ns1:Code"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="education-codes">
<xsl:with-param name="node-set" select="msxsl:node-set($deleted-nodes)/ns1:Code"/>
<xsl:with-param name="otherText" select="$educationType-nodes[last()]/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
<xsl:with-param name="status" select="'deleted'"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="education-codes">
<xsl:with-param name="node-set" select="$educationType-nodes/ns1:Code" />
<xsl:with-param name="otherText" select="$educationType-nodes/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</EducationTypes>
XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<ns1:ProgramInfo>
<ns1:RecognizedDegrees>false</ns1:RecognizedDegrees>
<ns1:EducationCodes>
<ns1:Code>01</ns1:Code>
<ns1:Code>02</ns1:Code>
<ns1:Code>09</ns1:Code>
<ns1:Code>10</ns1:Code>
<ns1:Code>12</ns1:Code>
</ns1:EducationCodes>
<ns1:OtherEducationTypeDesc>Old Description</ns1:OtherEducationTypeDesc>
<ns1:DegreeCodes>
<ns1:Code>03</ns1:Code>
<ns1:Code>06</ns1:Code>
<ns1:Code>07</ns1:Code>
</ns1:DegreeCodes>
<ns1:OtherDegreeDesc></ns1:OtherDegreeDesc>
<ns1:EducationLevels>
<ns1:Code>08</ns1:Code>
</ns1:EducationLevels>
<ns1:OtherEducationLevelDesc></ns1:OtherEducationLevelDesc>
</ns1:ProgramInfo>
<ns1:ProgramInfo>
<ns1:RecognizedDegrees>false</ns1:RecognizedDegrees>
<ns1:EducationCodes>
<ns1:Code>01</ns1:Code>
<ns1:Code>02</ns1:Code>
<ns1:Code>09</ns1:Code>
<ns1:Code>10</ns1:Code>
<ns1:Code>12</ns1:Code>
</ns1:EducationCodes>
<ns1:OtherEducationTypeDesc>New Description</ns1:OtherEducationTypeDesc>
<ns1:DegreeCodes>
<ns1:Code>03</ns1:Code>
<ns1:Code>06</ns1:Code>
<ns1:Code>07</ns1:Code>
</ns1:DegreeCodes>
<ns1:OtherDegreeDesc></ns1:OtherDegreeDesc>
<ns1:EducationLevels>
<ns1:Code>08</ns1:Code>
</ns1:EducationLevels>
<ns1:OtherEducationLevelDesc></ns1:OtherEducationLevelDesc>
</ns1:ProgramInfo>
Lösung
würde ich XMLUnit stattdessen empfehlen. XMLUnit ist so konzipiert, xmls vergleichen - http://xmlunit.sourceforge.net/ . Ich habe persönlich XMLUnit zum Vergleichen große (~ 10-20MB) verwendet XMLs statt XSLT verwenden.
Ich habe zusammen einen ANT-Wrapper um XMLUnit gelegt, so dass Verzeichnisse von xmls verglichen werden können und eine CSV-Ausgabe erstellt wird - https://github.com/parj/AddOnJavaAntTasks/tree/master/org.pm.xml.AntXMLUnit
Die JAR-Datei - https://github.com/parj/AddOnJavaAntTasks/downloads