The zero-or-one, one-or-more, and exactly-one Functions

Three functions relate specifically to static typing: zero-or-one, one-or-more, and exactly-one. These functions are useful when static typing is in effect, to override apparent static type errors.

Each of the functions takes a single argument and either returns the argument as is or raises an error if the argument is a sequence containing the wrong number of items. For example, when calling the zero-or-one function, if the argument is a sequence of zero or one items, it is returned. If it is a sequence of more than one item, an error is raised.

Earlier in this chapter, we saw how the expression:

number(doc("prices.xml")//prod[@num = 557]/price)

will cause a static error when static typing is in effect. This is because, as far as the processor knows, there could be more than one price element that matches that criterion, while the number function's signature requires that only zero or one item be provided. A static error can be avoided by using the expression:

number (zero-or-one(doc("prices.xml")//prod[@num = 557]/price))

In this case, no static error is raised. Rather, a dynamic error is raised if more than one price element is returned by the path expression. This is useful if you know that there will only be one product with number 557 in the document, and wish to override the static error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.106.33