Calling Some Functions with More Than One Node Causes an Error
In XSLT 1.0, there were a number of functions that took
a node as an argument. If we passed more than one node to the
function, it simply took the first node and ignored the rest. XSLT 2.0
is much more strict; passing more than one node to a function that
requires a single node is a fatal error. You can certainly make the
argument that using the argument /purchase-order/items/item when you really
mean /purchase-order/items/item[1]
is sloppy code. Whether you agree with that opinion or not, XSLT 2.0
forces you to pass a single node to the function.
The XSLT 1.0 and XPath 1.0 functions generate-id(), local-name(), name(), namespace-uri(), number(), and string() require a single node in XSLT
2.0. To avoid any problems, simply add the predicate [1] to your stylesheets as you migrate them.
In addition, if you use an XPath expression to select nodes for the
concat() function, that XPath
expression cannot return more than a single node. The expression
concat('#1. ', /report/brand/name) throws an error in
XSLT 2.0 if more than one node matches the XPath expression. On the
other hand, concat('#1. ',
(/report/brand/name)[1]) always works.
This is a dynamic error. It can be difficult to catch if your XPath expressions
typically select only one node. That means the error condition occurs
in the rare case that the XPath expression returns more than one node.
If you need only one node, changing your expression from x/y/z to (x/y/z)[1] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access