Conflict Resolution


When a pattern is used in the definition of a template rule, it is possible that several patterns may match the same node. There are rules for resolving this conflict, which are described in the section Choosing a Template Rule in the entry for <xsl:apply-templates> in Chapter 5 page 189. One of the factors these rules take into account is the default priority of the pattern, which is determined from the way it is written.

The default priority is decided according to the following rules. A numerically higher value indicates a higher priority. Note that some of the priorities are fractional , and some are negative.

If the pattern is a union of two or more patterns ( «P1 P2 » ), then the processor treats it as if there were two completely separate template rules specified, one for P1 and one for P2 , and it calculates the default priority of P1 and P2 independently, using the rules in the table.

If the pattern starts with an axis specifier ( «child:: », «attribute:: » , or «@ » ) this does not affect the priority.

Pattern Syntax

Default Priority

 document-node(schema-element(  QName  )) document-node(element(  QName,QName  )) schema-element(  QName  ) element(  QName,QName  ) schema-attribute(  QName  ) attribute(  QName, QName  ) 

+0.25

  QName  document-node(element(  QName  )) document-node(element(*,  QName  )) element(  QName  ) element(*,  QName  ) attribute(  QName  ) attribute(*,  QName  ) processing-instruction(  Literal  ) processing-instruction(  NCName  ) 

0.0

  NCName  :* *:  NCName  

-0.25

 / document-node() document-node(element()) document-node(element(*)} element() element(*) attribute() attribute(*) text() comment() processing-instruction() node() 

-0.5

Otherwise

+0.5

These default priorities are carefully chosen to reflect the selectivity of the pattern:

  • The patterns «node() » and «text() » and «* » are not very selective at all; they match any node of the right node kind, so they have a low priority of -0.5.

  • Patterns of the form «abc:* » or «@xyz.* » are more selective; they will match element or attribute nodes belonging to a particular namespace only, so they have a higher priority than the previous category. Patterns such as «*:abc » are also placed in this category, because they are less specific than a pattern that fully specifies the node name , but more specific than one that specifies only the kind of node.

  • Patterns such as «title » or «@isbn » are the ones most commonly encountered ; their default priority of 0.0 reflects the fact that in terms of selectivity, they are typical. Other patterns that specify the kind of node as well as its name fall into the same group . Patterns that specify the schema type of the node without specifying its name, for example «element(*,pers:employee) » are placed at the same level because they might be more or less specific than a pattern specifying the node name.

  • Patterns that specify both the node name and its schema type, for example «attribute(@code, mf:part-number) » are more specific than those that specify only the name or the type, so they get a priority of +0.25.

  • Patterns that provide a more specific context, for example «book[@isbn] » or «chapter/title » or «para[1] » have a higher priority, so they will be chosen in preference to templates whose patterns are respectively «book » , «title » , or «para » . Note however that this category can also include patterns that turn out not to be very selective at all, for example «//node() » .

All these values are chosen to leave you free to allocate your own priorities as natural numbers , for example «1 » , «2 » , «3: » , and such templates will always be chosen ahead of those with a system-allocated default priority.

You may find that stylesheets are easier to understand and less error prone if you avoid relying on default priorities, and use explicit priorities whenever you have more than one template rule that can match the same node.

Although the default priorities are carefully chosen, they do not guarantee that a highly selective pattern will always have higher priority than a less selective pattern. For example, the patterns «section/para » and «section/para[1] » both have priority +0.5. Similarly, the patterns «attribute(*, xs:integer) » and «attribute(*, xs:decimal) » have the same priority, even though the nodes that match the first pattern are a subset of those that match the second. Choosing your own priorities is therefore a more reliable approach.

A complete description of the conflict resolution rules, including the role played by the default priority of the pattern, is described under <xsl:apply-templates> page 189 in Chapter 5.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net