Subject: Re: [xsl] Simple XML Diff From: Michael Ludwig <mlu@xxxxxxxxxxxxx> Date: Fri, 11 Jul 2008 10:07:46 +0200 |
I'm trying to write an XML template that will compare two XML files that have *identical* structures.
If you can guarantee identical structure, elements can be compared based on their position in the document, and attributes can be compared based on the element they belong to and their name.
What I need to do is compare every element an attribute in new.xml with the equivalent in master.xml [...] What I'm stuck with is [...] how to iterate through all elements and attributes in new.xml and find the equivalent in master.xml.
<?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="master" select="document('master.xml')"/> <xsl:variable name="master-elms" select="$master//*"/>
<xsl:template match="/"> <xsl:for-each select="//*"> <xsl:variable name="melm" select="$master-elms[ position() ]"/> <!-- Text --> <xsl:value-of select="concat( name(), ' : ')"/> <xsl:choose> <xsl:when test=". = $melm">=</xsl:when> <xsl:otherwise>!</xsl:otherwise> </xsl:choose> <xsl:text> </xsl:text> <!-- Attribute --> <xsl:for-each select="@*"> <xsl:sort select="name()"/> <xsl:value-of select="concat( '@', name(), ': ')"/> <xsl:variable name="name" select="name()"/> <xsl:choose> <xsl:when test=". = $melm/@*[ name() = $name ]">=</xsl:when> <xsl:otherwise>!</xsl:otherwise> </xsl:choose> <xsl:text> </xsl:text> </xsl:for-each> </xsl:for-each> </xsl:template>
mludwig@forelle:~/Werkstatt/xsl > cat master.xml <Urmel u="U"> <eins>eins</eins> <zwei>zwei</zwei> <drei>drei</drei> <vier v="V">vier</vier> <x:fuenf xmlns:x="bla">fuenf</x:fuenf> </Urmel>
mludwig@forelle:~/Werkstatt/xsl > cat A.xml <Urmel u="U"> <eins>eins</eins> <zwei>zwei</zwei> <drei>dreiunddrei_ig</drei> <vier v="W">vier</vier> <x:fuenf xmlns:x="bla">fuenf</x:fuenf> </Urmel>
mludwig@forelle:~/Werkstatt/xsl > xsltproc Diff.xsl A.xml Urmel : ! @u: = eins : = zwei : = drei : ! vier : = @v: ! x:fuenf : =
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Simple XML Diff, Darcy Parker | Thread | Re: [xsl] Simple XML Diff, George Cristian Bina |
Re: [xsl] Validation XSLT using XSL, Ganesh Babu N | Date | Re: [xsl] Simple XML Diff, George Cristian Bina |
Month |