O'Reilly logo

Beginning XSLT and XPath: Transforming XML Documents and Data by Ian Williams

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix A. Answers to Exercises

This appendix contains suggested solutions to the exercises at the end of most of the chapters in this book.

No exercises

This chapter introduced XPath 2.0.

Question 1

Name some common XPath axes used to select element and attribute nodes.

Solution

Arguably the most commonly used axes are those that are used to select elements up and down the tree, probably in the following order. The child:: axis is implicit in the majority of selections, with the attribute:: axis coming in a close second.

  • child::

  • attribute::

  • parent::

  • descendant::

  • ancestor::

  • self::

Question 2

Assume that the context is in an element node. Write the expression that selects the xml:id attribute node for the element using the full and shortcut syntaxes (you can use any element name you like).

Solution

In this example I used the reference element at the root of the quick-reference documents.

<xsl:value-of select= "reference/@xml:id"/>
<xsl:value-of select= "reference/attribute::xml:id"/>

Question 3

After reviewing the string functions in XPath, create a simple XSLT stylesheet that you can use as a function test bed. Include several additional examples inside <p> elements using the following code as a guide:

<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform version="2.0"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <xsl:template match="/"> <output> <p> <xsl:value-of select="string-join(('my', 'string', 'join', string(4.00)), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required