4.18.  Comparing XFA Data

It does not make sense to compare the entire XFA data of two PDF documents, because often they contain creation date and modification data. For this reason PDFUnit provides an XPath based test method for XFA data.

Overview

There is only one powerful tag to test XFA data:

<!-- Tag to compare XFA data: -->

<haveSameXFAData />

Example - Resulttype Node

The result of the XPath expression has to be the same for both PDF documents:

<testcase name="haveSameXFAData_ResulttypeNode">
  <assertThat testDocument="test/test.pdf"
              masterDocument="master/master.pdf"
  >
    <haveSameXFAData>
      <matchingXPath expr="//default:pageSet" 
                     withResultType="NODE" 
                     defaultNamespace="http://www.xfa.org/schema/xfa-template/2.6/" 
      />
    </haveSameXFAData>
  </assertThat>
</testcase>

As you can see in the example the type of the expected result has to be specified. Possible result types for the attribute withResultType:

<!-- Result types for XPath-processing:  -->

withResultType="BOOLEAN"
withResultType="NUMBER"
withResultType="NODE"
withResultType="NODESET"
withResultType="STRING"

Example - Resulttype Boolean

The XPath-expressions may contain XPath functions:

<testcase name="haveSameXFAData_ResulttypeBoolean">
  <assertThat testDocument="test/test.pdf"
              masterDocument="master/master.pdf"
  >
    <haveSameXFAData>
      <matchingXPath expr="count(//default:field) = 3" 
                     withResultType="BOOLEAN" 
                     defaultNamespace="http://www.xfa.org/schema/xfa-template/2.6/" 
      />
    </haveSameXFAData>
  </assertThat>
</testcase>

Tests with an expected result type BOOLEAN are a problem because PDFUnit can't differentiate between not found and false.

A detailed description of how to work with XPath in PDFUnit can be found in chapter 8: “Using XPath”.

Example - Default Namespace

Be careful using the default namespace. It has to be declared in the attribute <haveSameXFAData />. (Because you can declare namespaces multiple times it could be ambiguous to detect the default namespace automatically.)

You can use the tag <matchingXPath /> more than once in a test. But it would be better to split the following test:

<!-- 
  Test with multiple XPath expressions.
  
  It is not recommended to create tests in this way.
  But PDFUnit has to ensure that it works.
-->
<testcase name="haveSameXFAData_MultipleDefaultNamespaces">
  <assertThat testDocument="test/test.pdf"
              masterDocument="master/master.pdf"
  >
    <haveSameXFAData>
      <matchingXPath expr="//default:agent/@name[.='designer']" 
                     withResultType="BOOLEAN"
                     defaultNamespace="http://www.xfa.org/schema/xci/2.6/" 
      />
      <matchingXPath expr="//default:subform/@name[.='movie']" 
                     withResultType="BOOLEAN"
                     defaultNamespace="http://www.xfa.org/schema/xfa-template/2.6/" 
      />
      <matchingXPath expr="//default:locale/@name[.='nl_BE']" 
                     withResultType="BOOLEAN"
                     defaultNamespace="http://www.xfa.org/schema/xfa-locale-set/2.7/" 
      />
    </haveSameXFAData>
  </assertThat>
</testcase>