older.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<produkt>
<code>000160</code>
<name>Product A (only in old list)</name>
<available>Yes</available>
<price>9,90</price>
</produkt>
<produkt>
<code>000161</code>
<name>Product B (price falls)</name>
<available>Yes</available>
<price>19,90</price>
</produkt>
<produkt>
<code>000163</code>
<name>Product D (price rises)</name>
<available>Yes</available>
<price>24,90</price>
</produkt>
<produkt>
<code>000164</code>
<name>Product E (price unchanged)</name>
<available>Yes</available>
<price>99,90</price>
</produkt>
</xml>
newer.xml
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<produkt>
<code>000161</code>
<name>Product B (price falls)</name>
<available>Yes</available>
<price>18,90</price>
</produkt>
<produkt>
<code>000162</code>
<name>Product C (only in new list)</name>
<available>Yes</available>
<price>16,90</price>
</produkt>
<produkt>
<code>000163</code>
<name>Product D (price rises)</name>
<available>Yes</available>
<price>28,90</price>
</produkt>
<produkt>
<code>000164</code>
<name>Product E (price unchanged)</name>
<available>Yes</available>
<price>99,90</price>
</produkt>
</xml>
比较.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="xml">
<xsl:variable name="older" select="document('older.xml')"/>
<xml>
<xsl:for-each select="produkt">
<xsl:variable name="code" select="code"/>
<xsl:variable name="findProduktByCode" select="$older/xml/produkt[code = $code]"/>
<xsl:if test="price != $findProduktByCode/price">
<produkt>
<xsl:copy-of select="code"/>
<xsl:copy-of select="name"/>
<xsl:copy-of select="available"/>
<oldprice>
<xsl:value-of select="$findProduktByCode/price"/>
</oldprice>
<newprice>
<xsl:value-of select="price"/>
</newprice>
</produkt>
</xsl:if>
</xsl:for-each>
</xml>
</xsl:template>
</xsl:stylesheet>
现在在 newer.xml 上应用 compare.xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<produkt>
<code>000161</code>
<name>Product B (price falls)</name>
<available>Yes</available>
<oldprice>19,90</oldprice>
<newprice>18,90</newprice>
</produkt>
<produkt>
<code>000163</code>
<name>Product D (price rises)</name>
<available>Yes</available>
<oldprice>24,90</oldprice>
<newprice>28,90</newprice>
</produkt>
</xml>