Validating XHTML5 in PHP?

三世轮回 提交于 2019-12-01 09:35:52

问题


Background

I have made the decision to serve my website as application/xhtml+xml to anyone who will accept it. I understand that there are consequences, which consequences I'm willing to accept.

Problem

I'm trying to validate XHTML5 in PHP. That way, I can serve the file as text/html if validation fails (hey, ain't nobody perfect). My question has merit even when taken outside of my situation, though.

So how what are my options to validate XHTML5?


What I've done:

I'm already using PHP's DOMDocument in my source code as a form of templating, so I'd like to be able to use DOMDocument::relaxNGValidate or DOMDocument::schemaValidate. I can find neither .xsd nor .rng to use. However, I think I have found a directory to make an rng grammar out of. Before today, I'd never even heard of Relax NG. Any help on this direction?


回答1:


As @Kevin Peno pointed out, HTML5 is not XML, and although that initially rubbed me the wrong way, I've come to terms with it and decided to treat HTML5 as a final output format only, instead of a document with the potential to be re-used and transformed again via XML tools.

My suggestion, if you wish to go down this path, is to pass your HTML5 markup through a cleanup process that guarantees valid XML (tools such as Tidy come to mind).

Your other option is to re-think your approach a bit and treat the HTML as a final output format. Use a different markup language to store content, and possibly even structure of your website.

For example, you could use Markdown for content (easily converted to HTML). If you prefer to stick with XML, perhaps define your own DSL, or re-use one such as Simplified DocBook. That has worked well for me in the past.

Then leverage XSL to transform your XML mash-up into HTML5, or any output format that you please.

Now, to try to answer your question directly, you will definitely need to "tidy" up the HTML5 markup to be sure it is valid XML before using a validation tool such as RelaxNG or XML Schema. As for your RNG schema file, I would suggest looking for an XHTML 1.0 Strict .rng first, and then perusing the HTML5 spec and adding elements and attributes as needed. A quick google search turned up the following.

In case you're deciding between RNG and XML Schema, save yourself some headaches and go RelaxNG. This is my opinion, but I've used both extensively and RelaxNG is so much more human-readable and easier to work with!




回答2:


The new (Nu) HTML5 Validator has RELAX NG schema for various presets including XHTML5

See https://bitbucket.org/validator/syntax/src/ed90a83ab0fd/relaxng https://github.com/validator/validator/tree/master/syntax/relaxng

Is this what you're looking for or am I confused?



来源:https://stackoverflow.com/questions/8100238/validating-xhtml5-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!