Loading login details...

Configuring DeltaXML to Ignore Elements

Using XSLT to ignore differences in specified elements or attributes

A common requirement for any change identification system is to ignore expected changes. For example, a changed Batch Number or Timestamp may be the only difference between two runs of a regression test suite. This article explains how to use XSLT to ignore elements and attributes.

For instructions on preserving information in original documents please see the article on Configuring DeltaXML to Handle Timestamps. Using this more sophisticated approach, when a genuine change is found the original timestamp information is still accessible.

Although this article assumes a working knowledge of XSLT, the approach described here can easily be implemented using your filtering mechanism of choice. These examples use XSLT rather than, for example, a proprietary configuration file, since this approach offers maximum flexibility, allowing complex rule sets to be handled easily. Pure-Java pipelines are also straightforward to implement.

We'll start with a simple example - we have two files to compare as shown below.

<?xml version="1.0"?> 
<top> 
 <element-to-ignore day="1" month="4"/> 
 <a/> 
 <b/> 
 <c/> 
 <d/> 
</top>
<?xml  version="1.0"?> 
<top> 
 <element-to-ignore day="10" month="5"/> 
 <a/> 
 <b/> 
 <c/> 
 <d/> 
</top>

We want to ignore the element <element-to-ignore> in both cases, so that no changes are reported between these two files. This can be achieved by adding a template to the input filter:

<!-- any elements that need to be ignored are matched here. 
To ignore an attribute, use @attribute-to-ignore in the match string. 
In this example we separate each item with a '|' delimiter as shown. --> 
<xsl:template match="element-to-ignore | another-element-to-ignore"/>

See "Using Filters with PipelinedComparator" in the Guide to Using Filters with DeltaXML  for instructions for using input and output filters.

As an example of the flexibility of this approach, imagine you wanted to ignore the <date> element if and only if it occurred within a <runDate> element - you could use this simple template:

<xsl:template match="runDate/date"/>

Perhaps you would like to see the date in your output file, with a note that it has been ignored. Simply change the template to this:

<xsl:template match="runDate/date"> 
<date>VALUE IGNORED</date> 
</xsl:template>

If you always want to ignore a date attribute, the following template will do this for you:

<xsl:template match="@date"/>

A more generic way to achieve this would be to have an attribute, say ignore-for-compare="true", on any elements you wanted to ignore in the comparison process. Use this template to remove such elements from the input stream so that changes to these elements are not reported:

<xsl:template match="*[@ignore-for-compare='true']"/>

Using XSLT we have a simple, versatile and standards-based mechanism for ignoring specific elements and attributes according to predefined rules. Please contact us if you have more complex requirements or would like sample XSLT code.