home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="text" encoding="ISO-8859-1"/>
- <!-- This stylesheet to be run against the 'source' xml document -->
- <!-- e.g. BooksA.xml -->
-
- <!-- can pass the other file by param -->
- <xsl:param name="compare-file-name" select="'booksB.xml'"/>
-
- <!-- setup vars to access the two docs regardless of context -->
- <xsl:variable name="second-src" select="document($compare-file-name)"/>
- <xsl:variable name="primary-src" select="/"/>
-
- <xsl:template match="/">
- <xsl:apply-templates select="books/book" mode="main"/>
- <!-- only thing left to check now is for Book -->
- <!-- elements in compare that are not present -->
- <!-- in the source doc -->
- <xsl:apply-templates select="($second-src)/books/book" mode="source-missing"/>
- </xsl:template>
-
- <!-- ============================================== -->
- <!-- main applied template for comparing the book -->
- <!-- elements between the two documents -->
- <xsl:template match="book" mode="main">
- <!-- look for the same book (i.e. with same isbn) in other document -->
- <xsl:variable name="other" select="($second-src)/books/book[@isbn = current()/@isbn]"/>
- <xsl:variable name="differences">
- <xsl:choose>
- <xsl:when test="count($other) = 0">
- <!-- no matching book element in the other document -->
- <xsl:text><book> element does not exist in comparison document
</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- <!-- compare A with B -->
- <xsl:apply-templates select="*">
- <xsl:with-param name="other" select="$other"/>
- <xsl:with-param name="other-file" select="'comparison document'"/>
- </xsl:apply-templates>
- <!-- and compare the other way (i.e. B with A) -->
- <xsl:apply-templates select="$other/*">
- <xsl:with-param name="other" select="."/>
- <xsl:with-param name="other-file" select="'source document'"/>
- <xsl:with-param name="is-other" select="true()"/>
- </xsl:apply-templates>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:variable>
- <xsl:if test="string-length($differences)">
- <xsl:text>Comparing Book ISBN #</xsl:text>
- <xsl:value-of select="@isbn"/><xsl:text>
</xsl:text>
- <xsl:value-of select="$differences"/>
- <xsl:text>======================
</xsl:text>
- </xsl:if>
- </xsl:template>
-
- <!-- ========================================== -->
- <!-- template to be applied to find differences -->
- <!-- between child elements of the book -->
- <xsl:template match="book//*">
- <xsl:param name="other"/>
- <xsl:param name="other-file"/>
- <xsl:param name="is-other" select="false()"/>
- <xsl:variable name="compare-node" select="($other)/*[name() = name(current())]"/>
- <xsl:choose>
- <xsl:when test="count($compare-node) = 0">
- <!-- element doesn't exist in the comparison -->
- <xsl:text><</xsl:text>
- <xsl:value-of select="name()"/>
- <xsl:text>> element does not exist in </xsl:text>
- <xsl:value-of select="$other-file"/>
- <xsl:text>
</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- <!-- check if element text is the same or diff -->
- <!-- NB. only compare one way otherwise the -->
- <!-- diff is output twice! -->
- <xsl:if test="not($is-other) and (./text() != $compare-node/text())">
- <xsl:text><</xsl:text>
- <xsl:value-of select="name()"/>
- <xsl:text>> value is different
</xsl:text>
- </xsl:if>
- <!-- do comparison of atts for this element -->
- <xsl:apply-templates select="@*">
- <xsl:with-param name="other" select="$compare-node"/>
- <xsl:with-param name="other-file" select="$other-file"/>
- <xsl:with-param name="is-other" select="$is-other"/>
- </xsl:apply-templates>
- <!-- do comparison of child elements for this element -->
- <xsl:apply-templates select="*">
- <xsl:with-param name="other" select="$compare-node/*"/>
- <xsl:with-param name="other-file" select="$other-file"/>
- <xsl:with-param name="is-other" select="$is-other"/>
- </xsl:apply-templates>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
-
- <!-- ============================================= -->
- <!-- template to be applied to find differences -->
- <!-- between attributes of the descendants of book -->
- <xsl:template match="book//@*">
- <xsl:param name="other"/>
- <xsl:param name="other-file"/>
- <xsl:param name="is-other" select="false()"/>
- <xsl:variable name="compare-att" select="($other)/@*[name() = name(current())]"/>
- <xsl:choose>
- <xsl:when test="count($compare-att) = 0">
- <!-- attribute doesn't exist in the comparison -->
- <xsl:text><</xsl:text>
- <xsl:value-of select="name(..)"/>
- <xsl:text>></xsl:text>
- <xsl:text> attribute @</xsl:text>
- <xsl:value-of select="name()"/>
- <xsl:text> does not exist </xsl:text>
- <xsl:value-of select="$other-file"/>
- <xsl:text>
</xsl:text>
- </xsl:when>
- <!-- NB. only compare one way otherwise the -->
- <!-- diff is output twice! -->
- <xsl:when test="not($is-other) and (. != $compare-att)">
- <!-- values of atts are different -->
- <xsl:text><</xsl:text>
- <xsl:value-of select="name(..)"/>
- <xsl:text>></xsl:text>
- <xsl:text> attribute @</xsl:text>
- <xsl:value-of select="name()"/>
- <xsl:text> value is different
</xsl:text>
- </xsl:when>
- </xsl:choose>
- </xsl:template>
-
- <!-- ============================================== -->
- <!-- this template just looks for books that appear -->
- <!-- in the compare doc but not in the source doc -->
- <xsl:template match="book" mode="source-missing">
- <xsl:variable name="primary" select="($primary-src)/books/book[@isbn = current()/@isbn]"/>
- <xsl:if test="count($primary) = 0">
- <xsl:text>Comparing Book ISBN #</xsl:text>
- <xsl:value-of select="@isbn"/>
- <xsl:text>
</xsl:text>
- <xsl:text><book> element does not exist in source document
</xsl:text>
- <xsl:text>======================
</xsl:text>
- </xsl:if>
- </xsl:template>
-
- </xsl:stylesheet>
-