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.49Mb size Format: txt, pdf, ePub
ads

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:

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

Other books

Enlightening Delilah by Beaton, M.C.
Reach for Tomorrow by Rita Bradshaw
Gone With the Witch by Annette Blair
A Southern Girl by John Warley
Before It's Too Late by Jane Isaac
Interior Design by Philip Graham
The Courtesan's Wager by Claudia Dain
The Wormwood Code by Douglas Lindsay