XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (49 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
3.22Mb size Format: txt, pdf, ePub

If the
$today
parameter is supplied as the value
2008-03-18
, the output would be as follows (regardless of the source document):

       xmlns:Date=“java:java.util.Date”>2008-03-18

The first namespace declaration is necessary, because it defines the namespace for the element name

. However, you probably don't really want the
xmlns:Date
declaration here. It's not doing any harm, but it's not doing any good either. It's there because the XSLT processor can't tell that it's unwanted. If you want this declaration to be omitted, use the
xsl:exclude-result-prefixes
attribute as follows:

   xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

   version=“2.0”

   xmlns:Date=“java:java.util.Date”

>


      

         

      




Alternatively, if your processor supports XML Namespaces 1.1, you can write
...
.

The fact that an element in the result tree has a namespace node does not necessarily mean that when the result tree is written out as an XML document, the corresponding element will have a namespace declaration for that namespace. The XSLT processor is likely to omit the namespace declaration if it is redundant, in other words, if it duplicates a namespace declaration on a containing element. It can't be omitted, however, simply on the basis that it is not used. This is because namespace declarations might affect the meaning of the data in the output document in a way that the XSLT processor is unaware of. Applications are perfectly entitled to use namespace declarations to scope identifiers and names appearing in attribute values or text.

The
xsl:exclude-result-prefixes
attribute is used to remove namespace declarations that are unused and unwanted. It can't be used to remove the declaration of namespace prefixes that are actually used in the result tree. And it isn't used to remove duplicate namespace declarations, as most processors will do that automatically.

Namespace Inheritance

By default, when an element is generated in the result tree, it will automatically acquire copies of the namespace nodes attached to its parent in the result tree. Suppose your stylesheet has the following form:


  

    

  



  This product is brilliant!


The result of applying these two rules to a

element in the source document will generally look like this:


    This product is brilliant!


Note that in this serialized output, the namespace
urn:acme-com:product
is in scope for the

element. Suppose, however, that you are using XML Namespaces 1.1, and you want the result tree to be serialized as follows:


    This product is brilliant!


To achieve this, you need to do two things. Firstly, you need to ensure when constructing the

element that it doesn't acquire a namespace node for the
urn:acme-com:product
namespace. Secondly, you need to ensure that the serializer generates the namespace undeclaration
xmlns:p = “”
.

The first step is achieved by setting the attribute
xsl:inherit-namespaces = “no”
on the

literal result element. This switches off the normal behavior during tree construction, by which a child element automatically inherits the namespaces of its parent element.

Other books

Echoes of the Past by Susanne Matthews
You Knew Me When by Emily Liebert
2021 by Martin Wiseman
Origins (A Black Novel, #1) by Jessa L. Gilbert
Chorus Skating by Alan Dean Foster
Revealed by Kate Noble
Hard Ride by Trixie Pierce
Demon Day by Penelope Fletcher