Connecting Tech Pros Worldwide Help | Site Map

Validate (X)HTML with custom tags with PHP DOM API

Gulzor's Avatar
Newbie
 
Join Date: Jul 2008
Location: Brussels, Belgium
Posts: 27
#1: Dec 17 '08
Hi,

I have defined a set of custom tags that I put among regular HTML tags. E.g.

<body>
<div id="regular_id">
<namespace:container id="something" type="article" />
</div>
</body>

At this time, I use PHP preg_* functions to parse the HTML file and trigger actions when I detect a tag that starts with the string "namespace:".

It works but I feel that I can do better with PHP DOM API :
Question : am I right on this ?

1) I would like to create either a DTD or a Schema for my custom tags and validate my HTML file against this.

I thought I could modify the official DTD for XHTML1.1 (transitional) and define my custom tags in it.

But then I might lost the benefits of namespaces since my custom tags will be merged with the regular HTML tags ?

2) I would like to use DOMDocument::getElementsByTagNameNS() to retrieve all my custom tags.

I don't know what's the best strategy.

All advises appreciated, code sample also ;-)

Thank you.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Dec 17 '08

re: Validate (X)HTML with custom tags with PHP DOM API


Quote:

Originally Posted by Gulzor View Post

I thought I could modify the official DTD for XHTML1.1 (transitional) and define my custom tags in it.

there is no XHTML 1.1 transitional DTD (there's only one DTD for XHTML 1.1)

you can write your own DTD using the official one as a base or extend/redefine some of the official elements. the question is, if the browsers support that (a question to Dr. Google).

Quote:

Originally Posted by Gulzor View Post

But then I might lost the benefits of namespaces since my custom tags will be merged with the regular HTML tags ?

you wouldn't. a DTD simply checks, if a document conforms to a given structure. not more, not less. the benefit of namespaces is not affected by a DTD.

Quote:

Originally Posted by Gulzor View Post

At this time, I use PHP preg_* functions to parse the HTML file and trigger actions when I detect a tag that starts with the string "namespace:".

if these elements only trigger some server side action, remove them before you send the document to the browser. all that will be left is XHTML.

Quote:

Originally Posted by Gulzor View Post

2) I would like to use DOMDocument::getElementsByTagNameNS() to retrieve all my custom tags.

sounds reasonable. code samples you'll find here

regards

note: IE doesn't support XHTML (i.e. it doesn't support the XHTML MIME type (and probably never will)) so make sure IE will be given an according document. (if you have XHTML 1.1 you probably know, what MIME type should be served (if not, see XHTML Media Types))
Reply

Tags
dom, dtd, php, schema, xml