p:compare — Compares two documents for equality.
<p:declare-step
type
="
p:compare
"
>
<p:input
port
="
source
"
primary
="
true
"
/>
<p:input
port
="
alternate
"
/>
<p:output
port
="
result
"
primary
="
false
"
/>
<p:option
name
="
fail-if-not-equal
"
select
="
'false'
"
/>
<!--
boolean -->
</p:declare-step>
The p:compare step compares two documents for equality. Two documents are considered equal if they are equal according to the semantics of the deep-equal function (see [XPath 2.0 Functions and Operators]).
The step returns a c:result element that contains either true or false indicating whether or not the documents were equal.
If the fail-if-not-equal option is true, the step fails if the documents are not equal.
This pipeline compares its single input document against a fixed alternative. It returns true if they're the same.
1 <p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="1.0"> <p:input port="source"/> <p:output port="result"> 5 <p:pipe step="compare" port="result"/> </p:output> <p:compare name="compare"> <p:input port="alternate"> 10 <p:inline> <doc> <para>First para.</para> <para>Second para.</para> <para>Third para.</para> 15 </doc> </p:inline> </p:input> </p:compare> </p:declare-step>
Input | → | Output |
---|---|---|
1 <doc>
<para>First para.</para>
<para>Second para.</para>
<para>Third para.</para>
5 </doc>
|
1 <c:result xmlns:c="http://www.w3.org/ns/xproc-step">true</c:result>
|
It returns false if they're different.
Input | → | Output |
---|---|---|
1 <doc>
<para>Not the same.</para>
</doc>
|
1 <c:result xmlns:c="http://www.w3.org/ns/xproc-step">false</c:result>
|
If fail-if-not-equal is “true”, then the step fails when the documents are different.
1 <p:pipeline xmlns:p="http://www.w3.org/ns/xproc" version="1.0"> <p:try> 5 <p:group> <p:compare fail-if-not-equal="true"> <p:input port="alternate"> <p:inline> <doc> 10 <para>First para.</para> <para>Second para.</para> <para>Third para.</para> </doc> </p:inline> 15 </p:input> </p:compare> <p:identity> <p:input port="source"> <p:inline> 20 <PASS/> </p:inline> </p:input> </p:identity> </p:group> 25 <p:catch> <p:identity> <p:input port="source"> <p:inline> <FAIL/> 30 </p:inline> </p:input> </p:identity> </p:catch> </p:try> 35 </p:pipeline>
Input | → | Output |
---|---|---|
1 <doc>
<para>Not the same.</para>
</doc>
|
1 <FAIL/>
|
And succeeds if they're the same.
Input | → | Output |
---|---|---|
1 <doc>
<para>First para.</para>
<para>Second para.</para>
<para>Third para.</para>
5 </doc>
|
1 <PASS/>
|
The p:compare step is very sensitive to changes in the document. Changes in the leading and trailing whitespace around the document element, for example, is enough to make the documents different. As shown, the “pretty printed” examples in this book would actually be different because there is extra whitespace inside the p:inline elements.
If you look at the source for the pipelines, you'll see that we very carefully made the whitespace the same.