1. XSLT Variables

Variable 1

<xsl:variable name="cost">
	<xsl:value-of select="sum(/statement/item/amount)"/>
</xsl:variable>

Variable 2

<xsl:variable name="cost" select="sum(/statement/item/amount)"/>

1.1 What is the difference between Variable 1 & 2 above?

Variable 1 holds a result tree fragment with a root node and a text node with string value the decimal representation of a number Variable 2 holds a number

2 points

1.2 Why would you use one rather than the other?

Variable 2 is simpler and much easier to use in XSLT 1.0

1 points

1.3 What is special about XSLT Variables?

Variable are assigned once and cannot have their value changed

2 points

1.4 Why is this behaviour thought to be desirable?

Transparency - it is far easier to reason about a program that is free from side effects. So you can prove your program is correct with less effort. Scalability - pure (side-effect free) programs are much easier to parallelize Debugging - no need to monitor when a variable changes value.

3 Points

<things>
	<thing>
		<quantity>1</quantity>
		<price>2</price>
	</thing>
	<thing>
		<quantity>4</quantity>
		<price>5</price>
	</thing>
	<thing>
		<quantity>3</quantity>
		<price>10</price>
	</thing>
	<thing>
		<quantity>2</quantity>
		<price>1</price>
	</thing>
</things>

1.3 Please declare and initialise a variable that can be used to total the cost of things.

<xsl:variable name="x"> <xsl:for-each select="things/thing"> <xsl:value-of select="quantity * price"/> </xsl:for-each> </xsl:variable>

4 points

©Wiley Interscience