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

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

The xpath-default-namespace Attribute

A common source of bewilderment for XSLT 1.0 users is when they see a source document that looks like this:

Some text here

and they try to match this with a template rule like this:


or perhaps to output the value with an expression like this:


and nothing happens. Only with experience do you learn to look near the top of the document for a tell-tale default namespace declaration:


Sometimes the default namespace declaration is even more carefully hidden, being placed as a default attribute value in the DTD. The XHTML specification is an example that uses this infuriating technique: hiding in its DTD is the snippet:


  %i18n;

  id ID #IMPLIED

  xmlns %URI; #FIXED ‘http://www.w3.org/1999/xhtml’

>

which has the effect that every element you use in a document that invokes this DTD, an element written as


perhaps, is actually in the namespace
http://www.w3.org/1999/xhtml
although you probably didn't know it.

When you write
match=“br”
or
select=“br”
in your stylesheet, you are asking for


elements in the null namespace, and you will not match


elements in any non-null namespace. To match elements in the namespace
http://www.w3.org/1999/xhtml
, you first have to declare this namespace in your stylesheet (with a specific prefix):

   xmlns:xhtml=“http://www.w3.org/1999/xhtml”>

and then use this prefix whenever you refer to elements in this namespace:
match=“xhtml:br”
and
select=“xhtml:br”
.

Effect

XSLT 2.0 hasn't found any way of eliminating the frustration of not realizing that the source elements were actually in a namespace, but it has eliminated the tedium of adding all the prefixes to your patterns and XPath expressions once you've realized what the problem is. You can now declare the namespace as being the default namespace for elements in XPath expressions by writing:

Other books

It Had to Be You by Susan Elizabeth Phillips
Bones and Ashes by Gemma Holden
Bowl of Heaven by Gregory Benford and Larry Niven
Complete Works, Volume I by Harold Pinter