XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / Summary and exercises / Answers to review questions

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)

Question 1: For stylesheet modules imported with xsl:import...

a) … the first imported module takes precedence.

b) … the last imported module takes precedence.

c) … both modules have equal priority.

Answer b). The last imported module, together with its entire direct and indirect import branch, has higher import precedence than modules imported earlier. Imports cannot be simultaneous, so identical rules of equal import precedence normally cannot occur unless inclusion is also involved.

Question 2: For modules included with xsl:include...

a) … the first rule wins a conflict.

b) … the last rule wins a conflict.

c) … both rules apply because import precedence does not.

Answer b). XSLT processors conventionally use the last rule. Whether it is local or included depends entirely on the include instruction’s position. Even without import precedence, two identical rules cannot both remain active. An included rule, like a local rule, always outranks an imported rule.

Question 3: What matters for named templates?

a) A template with the called name must exist.

b) Attribute mode cannot be used in the call.

c) A named template must not have attribute select.

d) A parameter can be passed.

Answers a) and d). Calling a named template with xsl:call-template fails if no such template exists. It may also have a select or mode attribute to double as a template rule. A passed parameter, unless it is a tunnel parameter, should have a corresponding xsl:param declaration to receive it.

Question 4: A global parameter …

a) … is always declared in a template rule.

b) … has a default value.

c) … can receive a new value during execution.

d) … retains its value throughout execution.

Answer d). A global parameter’s value remains unchanged throughout execution. An explicit default is not required; normally a value comes from outside. Without a default, select attribute or sequence constructor, its value becomes the empty string. Parameters declared inside template rules are always local.

Question 5: For two variables with the same name …

a) … the local variable hides the global one.

b) … the global variable is always visible.

c) … the last declared variable applies.

d) … two local variables cause an error.

Answer a). A local variable hides a same-name global variable for its template rule’s duration. Two same-name globals with equal import precedence always cause an error. This is not necessarily true of locals, but avoid declaring a variable twice in one template rule.

Question 6: Which statements about id() and generate-id() are correct?

a) id() can find identifiers declared in the DTD.

b) id() can find identifiers generated by generate-id().

c) generate-id() can generate multiple identifiers per element.

d) generate-id() always generates the same value for a node.

Answers a) and d). Although id() and generate-id() have similar names, their tasks differ. Function id() finds nodes by their real, explicitly declared identifier values. Function generate-id() generates a runtime identifier constant for a given node. Neither can generate-id() access real IDs, although it can add a runtime ID to a node already having one, nor can id() meaningfully access purely virtual generate-id() values.

Question 7: Which statements about keys are correct?

a) Multiple same-name key lists can exist.

b) One node can have several key values.

c) A key must be declared in the DTD.

d) A key must be in the main stylesheet.

Answer b). Keys are tables created by the XSLT processor containing node pointers; a node may appear several times. Generated during or at the start of processing, they have nothing to do with a DTD. Same-name key lists cannot remain separate: they always merge into one, rather than conflict. Declaration in the main module or elsewhere makes no difference.

   

back to top