XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (543 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.62Mb size Format: txt, pdf, ePub
ads

If two items in corresponding positions are nodes, then to be deep-equal they must satisfy a number of conditions:

  • They must be the same kind of node (for example, both elements or both text nodes).
  • They must have the same name, that is, the same namespace URI and the same local name, or they must both be unnamed nodes such as text nodes.
  • In the case of document nodes, and element nodes whose type allows one or more element children, the sequences of children for the two nodes must be deep-equal to each other, after discarding any comments and processing instructions.
  • In the case of element nodes, there must be a one-to-one correspondence between the attributes of the two elements (same attribute name, and typed values that are deep-equal).
  • In the case of attribute nodes and element nodes whose type does not allow element children, the typed values must be deep-equal to each other.
  • In the case of text nodes, comments, processing instructions, and namespace nodes, they must have the same string value, compared using the selected collation. Note however that comments, processing instructions, and namespace nodes are only taken into account if they occur directly as items in the sequences supplied as arguments to the
    deep-equal()
    function. When they occur within the content of an element node, they are not considered.

Nodes can be deep-equal even if they differ in certain respects:

  • When comparing elements, the namespace nodes of the elements do not need to be the same, and contained comments and processing instructions are not taken into consideration. (The fact that the namespace nodes can be different also means that one element can pass validation while the other fails validation, if they happen to contain
    xs:QName
    values in their content.)
  • Type annotations are not taken into account; for example, two attributes can be equal if one is annotated as an
    xs:decimal
    with value 3.0, and the other is annotated as
    xs:integer
    with value 3.
  • The order of attributes within an element can vary. (But the order of attribute nodes in the top-level sequence is significant.)
  • The base URI can vary.
  • When comparing document nodes, the document URI is ignored, as are unparsed entities.

Surprisingly, however, whitespace text nodes are taken into account even within an element that has an element-only content model. Furthermore, although comments and processing instructions appearing as children of an element are not compared directly, their presence can affect the result because they split a text node into two: so
123456
and
123456
are not deep-equal.

Examples

Expression
Result
deep-equal((1,2,3), (1,2,3))
true
deep-equal((1,2,3), (3,2,1))
false
deep-equal((1,2),
(1.0, 2.0))
true
deep-equal((),
())
true

In the following examples, assume that
$doc
refers to the following document:


   

   

   


and assume that
$weak
refers to a collation under which
c
and
C
compare as equal. Then:

Expression
Result
deep-equal($doc/e[1], $doc/e[2])
false
deep-equal($doc/e[1], $doc/e[3], $weak)
true
deep-equal($doc/e[1]/@*, $doc/e[2]/@*, $weak)
Undefined (the result depends on the order of
attribute nodes
,
which is unpredictable)

Usage

The
deep-equal()
function represents one particular way of deciding whether two nodes or sequences are equal to each other. In practice there are probably two common ways it is likely to be used:

  • To compare two sequences of atomic values: the result is true if the two sequences are the same length, and the sequences are pairwise equal to each other.
  • To compare two element or document nodes to see if they have the same content at every level of the hierarchy.

Note that comparing two element nodes using the
=
or
eq
operators fails if the elements are defined in the schema to have a complex type, unless this is a complex type allowing mixed content, in which case the elements are compared by comparing their string values.

The definition of deep equality for nodes is one that will suit some tastes and not others. For example, it treats comments and processing instructions within an element as insignificant, but whitespace between elements as significant. It also treats the order of child elements (but not attributes) as significant. If you don't like this definition, the answer is simple: define your own function, and use that instead.

Saxon provides a function
saxon:deep-equal()
which is modeled on the standard
deep-equal()
function, but provides an extra argument allowing user control over the way in which the comparison is performed. Details are at
http://www.saxonica.com/documentation/extensions/functions/deepequal.html
.

default-collation

The
default-collation()
function returns the URI of the default collation, that is, the collation that is used when no collation is explicitly specified in a function such as
compare()
.

Signature

This function takes no arguments.

Type
Meaning
Result
xs:string
The URI of the default collation from the runtime context

Usage

The
default-collation()
function is useful when you want to assign a collation conditionally, for example:

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
9.62Mb size Format: txt, pdf, ePub
ads

Other books

This Red Rock by Louise Blaydon
Dark Plums by Maria Espinosa
Planet of Dread by Murray Leinster
The Carpenter's Children by Maggie Bennett
Once Upon a Wallflower by Wendy Lyn Watson
House of Masques by Fortune Kent
Connie Mason by A Touch So Wicked