XSLT Assessment Questions

Author:
Aidan Killian, Martin Dransfield, Warrell Harries
For:
Wiley Interscience/SCS
Date:
August 2007

Assessment of XSLT Knowledge

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

2. XSLT Features

2.1 Please summarise the difference between 'Push' and 'Pull' usage in XSLT?

A 'Pull' implementation prefers the use of <xsl:call-template> over the use of <xsl:apply-templates>

2 points

2.2 Please describe four differences between XSLT v1.0 and v2.0?

New options for the grouping of nodes: the xsl:for-each-group instruction and the current-group() function have been added. Users can create and use custom functions in the stylesheet The possibility of generating multiple output documents have been extended in comparison to XSLT 1.1. You can also link between these 'result trees'. XHTML output method has been added. There is a new xsl:sort-key declaration. The possibility to create a temporary tree on which to perform actions. A new instruction xsl:namespace has been added to generate namespace nodes.

4 points

3. Template Usage

<xsl:template name="proc">
   <xsl:param name="txt" select="''"/>
   <value>
      <xsl:choose>
         <xsl:when test="contains($txt, '|')" >
            <xsl:value-of select="substring-before($txt, '|')"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$txt"/>
         </xsl:otherwise>
      </xsl:choose>
   </value>
   <xsl:variable name="left" select="substring-after($txt, '|')"/>
   <xsl:if test="string-length($left)>1" >
      <xsl:call-template name="proc" >
         <xsl:with-param name="txt" select="$left"/>
      </xsl:call-template>
   </xsl:if>
</xsl:template>

3.1 In the template above name the computing technique being used?

Recursion

1 points

3.2 Please give one benefit and one disadvantage of this technique

Benefit Generates new variables on each iteration Disadvantage Resource hungry

2 points

<xsl:template match="h:parameter[@name='order']/h:value">
   <xsl:variable name="values">
      <xsl:call-template name="proc" >
         <xsl:with-param name="txt" select="text()"/>
      </xsl:call-template>
   </xsl:variable>
   <xsl:for-each select="xalan:nodeset($values)/value">
      <SetGroupTitleJournalOrder group="1" title="{.}" sort="{position()}"/>
   </xsl:for-each>
</xsl:template>

3.2 In the XSL 1.0 template above describe the purpose of xalan:nodeset?

It's a XalanJ extension function that allows processing of result tree fragments in XSLT 1.0

3.3 Why is the following stylesheet often used by XSL developers?

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________

4. XPath

4.1 When might you use current() rather than '.' when selecting a node?

______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________

xmlTree.jpg

4.2 Please write an XPath expression that returns the contents of all p elements that follow the one with ID="p13"

______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________ ______________________________________________________________________________________

©Wiley Interscience