XPath Problem in XHTML Files

Having trouble installing Oxygen? Got a bug to report? Post it all here.
Melander

XPath Problem in XHTML Files

Post by Melander »

I've been trying to transform some valid XHTML strict files using XSLT.

In straight edit mode, if I include a DOCTYPE declaration (whether the local Oxygen version or the online W3C one) the transformation does not work as I would anticipate (whether I use Saxon or Xalan) and I get some odd results in the XPath console.

When the DOCTYPE is declared, the XPath query "/html" gets me a dialog box informing me that "The XPath query returned no results". If I remove the DOCTYPE declaration, the query gives me the html element as one would expect. Further, regardless of DOCTYPE presence or absence, the same query in Tree Editor mode gives me the html element.

However, the presence of the DOCTYPE does not entirely cripple XPath. So a query of "//*" returns all elements and a query of "//@href" returns all href attributes. So the odd result seems to apply only when I query for elements by name and seems not to apply to attributes.

My desire for DOCTYPEs is not just HTML snobbery, but has real world implicatons for me, since DOCTYPEs are important for persuading the Explorer 6 browser to act no more foolish about CSS than it has to.

Is this an XHTML thing, an XSLT thing, a parser thing or an Oxygen thing? Is there a workaround that would allow me to keep my DOCTYPEs?

Thanks in advance for any help you can give.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Melander,

The html element of an XHTML document should belong to http://www.w3.org/1999/xhtml namespace. Now when you have your DTD declaration this is achived because of the xmlns fixed attribute, see below the html definition from the DTD:

Code: Select all


<!ELEMENT html (head, body)>
<!ATTLIST html
%i18n;
id ID #IMPLIED
xmlns %URI; #FIXED 'http://www.w3.org/1999/xhtml'
>
When you remove the DTD declaration then you do not have an XHTML document anymore as there is nothing to bind the elements to the http://www.w3.org/1999/xhtml namespace. To achieve a similar behavior I would recommend to add the xmlns attribute explicitelly to the html element from your XHTML document.


The /html XPath gives you results when you remove the DTD declaration because as I explained earlier in this case the html root element belongs to no namespace. In order to match an element from a specified namespace you should bind that namespace to a prefix ( xmlns:xhtml="http://www.w3.org/1999/xhtml" for instance) and use that prefix to qualify the element names (xhtml:html for instance). Alternatively you can use the local-name [1] function and have something like /*[local-name()='html'] instead of /html for instance, but I guess using a namespace prefix is simpler.

[1] http://www.w3.org/TR/xpath#function-local-name

Hope my answer clarifies things for you.

Best Regards,
George
Melander
Posts: 1
Joined: Wed Nov 26, 2003 1:41 am

Post by Melander »

Thanks for your help George. Didn't even occur to me that there was a default namespace.

As far as I can figure out, since XPath doesn't know from default namespaces, I have to type xhtml: in front of every step in the XPath expression.

Between that and the longwinded methods in the DOM, I'm convinced W3C is trying to give me carpal tunnel!!!

Regards,
Post Reply