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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gulzor
    New Member
    • Jul 2008
    • 27

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

    Hi,

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

    <body>
    <div id="regular_id" >
    <namespace:cont ainer 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::ge tElementsByTagN ameNS() to retrieve all my custom tags.

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

    All advises appreciated, code sample also ;-)

    Thank you.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by Gulzor
    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).

    Originally posted by Gulzor
    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.

    Originally posted by Gulzor
    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.

    Originally posted by Gulzor
    2) I would like to use DOMDocument::ge tElementsByTagN ameNS() 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))
    Last edited by Dormilich; Dec 17 '08, 10:00 AM. Reason: added note for IE

    Comment

    Working...