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
4.78Mb 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

A Little Bit Wicked by Rodgers, Joni, Chenoweth, Kristin
For Eric's Sake by Carolyn Thornton
A Pretext for War by James Bamford
Jane Two by Sean Patrick Flanery
Fallen by Lauren Kate
No One in the World by E. Lynn Harris, RM Johnson
Home Run: A Novel by Travis Thrasher
Poison by Kathryn Harrison
The Alpine Yeoman by Mary Daheim