xsl-list
[Top] [All Lists]

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

2003-03-19 11:53:31
Hi Lars,

You're steaming through these! :)

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.

Note that you'll need to use an identity template in addition to the
template above in order to copy the attributes and the non-XHTML
elements that are nested within the XHTML element.

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.

The literal text doesn't need to be within a literal result element.
It could be anywhere except while a <xsl:attribute>, <xsl:comment> or
<xsl:processing-instruction> instruction is active. You can have text
nodes generated at the top of the tree if you want.

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.

There's also using <xsl:number> but that's not introduced until
Chapter 9.

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.

The <xsl:strip-space> element only controls how whitespace-only text
nodes are treated in the *source* document. It is always the case (no
matter what you have in <xsl:strip-space> that whitespace-only text
nodes in the stylesheet, aside from those within an <xsl:text>
element, will be ignored.

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.

Also if the value of the attribute needs to be generated with XSLT
instructions rather than an XPath expression. For example, if you want
the value of the attribute to be conditional, or if you want it to be
generated by applying templates to something.

Another time might be if you want to create an attribute that you
don't want to be recognised by an application processing the
stylesheet, for example an xsi:type attribute that you want to appear
in the result, not be used to determine the type of an element when
validating the stylesheet.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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