xsl-list
[Top] [All Lists]

Answers to review questions in "Beginning XSLT": Chapter 8

2003-03-18 13:38:26
Hi all,
Here are my answers to the review questions in Ch. 8 of "Beginning XSLT"
by Jeni Tennison.  Comments, corrections or clarifications are appreciated.
My hope is that these postings and responses will be of value in the future
to anyone working through Beginning XSLT and wanting to check their answers.

Regards,
Lars



1. What are the two ways in which you can add an element node to the
   result treee?  What are the advantages and disadvantages of each?

Answer:

Literal result elements, and <xsl:element> instructions.

LRE's are easier to read and shorter to type, and they allow you
to specify attributes and their values literally as well (instead
of using <xsl:attribute>, as you have to with an <xsl:element>).

<xsl:element> instructions allow you to create elements with dynamic
names and/or namespaces.


2. What function can you use to change the case of a string?

Answer: translate(string, from-characters, to-characters)


3. What else do you need to know to work out what namespace the
   elements generated by the following instructions are in?

Answers:

<html>...</html> -- the default namespace in scope in the stylesheet
   at this point.
<xsl:element name="html">...</xsl:element> -- same as above.
<xsl:element name="{$prefix}:html">...</xsl:element> -- the value of
   the variable 'prefix', and the namespace to which it is bound at
   this point in the stylesheet.  (Note, the request to use $prefix
   as the actual prefix in the output may not be honored, but a prefix
   bound to the same stylesheet will be used.)
<xsl:element name="html" namespace="{$namespace}">...</xsl:element> --
   the value of the variable 'namespace'.


4. Write a template that matches any element in the XHTML namespace and
   generates a copy of that element in no namespace.

Answer:

  <xsl:template match="xhtml:*">
    <xsl:element name="{local-name()}" namespace="">
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

assuming that the xsl:stylesheet element has the namespace declaration
   xmlns:xhtml="http://www.w3.org/1999/xhtml";
on it.
The above is designed to recursively copy all content and
attributes of the element as well as the element itself.


5. What are the three ways in which you can add a text node to the result tree?

Answer:

1) Using literal text in a literal result element.
2) Using <xsl:value-of select="..." />
3) Using <xsl:text>...</xsl:text>
4) Using <xsl:copy> or <xsl:copy-of> to copy a text node from the source tree.


6. What are two reasons you might have for using <xsl:text> rather than
   literal text within a template?

Answer:

1) to separate text you want added to the result tree from whitespace
   that's only there to make the stylesheet readable.
2) to create whitespace-only text nodes that are <xsl:strip-space>-proof.


7. In what situations would you use <xsl:attribute> rather than adding an
   attribute literally to a literal result element?

Answer:

1) When you want the attribute's name or namespace to be determined 
   dynamically.
2) When you want the attribute to be created conditionally (i.e. it
   may or may not be present depending on certain runtime conditions).
3) Within an <xsl:element>, you can't use a literal attribute to add
   attributes to the element.


8. Look at the following piece of XSLT.  What value will the class attribute
   on the <p> element have?

   <xsl:template match="Program">
     <p xsl:use-attribute-sets="program show"
     class="{local-name()}">
    <xsl:attribute name="class">episode</xsl:attribute>
     </p>
   </xsl:template>

   <xsl:attribute-set name="program">
     <xsl:attribute name="class">program</xsl:attribute>
   </xsl:attribute-set>

   <xsl:attribute-set name="show">
     <xsl:attribute name="class">show</xsl:attribute>
   </xsl:attribute-set>

Answer:

  'episode', since <xsl:attribute> overrides literal attributes
  as well as attribute sets.


9. When would you use an identity template to copy a branch of the source tree
   rather than <xsl:copy-of>?

Answer:

When you want to copy most of the branch as is, but need to change or
process certain parts of it specially.  An identity template provides
the copying facility but lets you override the identity template with
other templates for specific categories of nodes.


10. What output method should you use to generate XHTML?  What other attributes
    do you need to set on <xsl:output> to create a conformant XHTML document?

Answer:

Use the "xml" output method, as well as the attributes
   doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
   doctype-system="DTD/xhtml-strict.dtd"

It is also recommended to use
   media-type="text/html"
although this is not required in order to create a conformant XHTML document.



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list