0121 31 45 374
Qoute Icon

ASP.Net 2.0 XHTML Valid? It is unless you want to use the W3C validator

Tim

I came across an interesting anomaly last week with one of our new sites (http://www.missmays.com/ -not work safe). We first started developing the site in ASP.Net 1.1 and wanted to make it XHTML 1.1 valid so wrote a HttpModule that transforms the offending source (i.e. wrapping inputs in div tags). Over time we realised that we could also encode other offending items such as ampsands which would save us a load of time typing them out in the URL’s and updating the database.

We knew the site’s HTML was XHTML1.1 valid and the tests we’d run with the source were fine but on validating the live site using the W3C URL Validator (http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.missmays.com%2F) all was not so rosey. It was falling over on the last two inputs (__PREVIOUSPAGE and __ EVENTVALIDATION). On examing the module code it turned out that I hadn’t included them in the Regex (new to ASP.Net 2.0). Adding them all was fine, but why doesn’t the framework automatically wrap the inputs in div tags as it does for the browsers?

Checking the main ASP.Net site (http://www.asp.net/) the same thing happens, looking at the source through Firefox or IE shows the inputs wrapped nicely but through the validator they’re not, the source isn’t XHTML1.0 Strict anyways btw but it’s still worth noting that validating the URL fails on different points: http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fasp.net%2F

I’d be interested to know if anyone else has come across this before or has an insight on why it may be happening.

This is what the validator used to produce when validating against the URL:

This page is not Valid XHTML 1.1!

Below are the results of checking this document for XML well-formedness and validity.

  1. Error Line 327 column 96: document type does not allow element "input" here; missing one of "ins", "del", "h1", "h2", "h3", "h4", "h5", "h6", "p", "div", "address", "fieldset" start-tag.
...GE" value="CLXotzBdGnifesOhxidubQ2" /> 

The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.
One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").

  1. Error Line 328 column 183: document type does not allow element "input" here; missing one of "ins", "del", "h1", "h2", "h3", "h4", "h5", "h6", "p", "div", "address", "fieldset" start-tag.
...K6hIzLASsITXlU6vlPxFkdzgJFxH9ADe8B" /></form> 

The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>"

Update: Although I've not yet had a chance to try it, Phil Winstanley has suggested using .browser files may get around this.

Update 2: Here's a browser file you can use, you'll need to add a "Special ASP.Net 2.0 Directory" in your solution calls App_Browsers and place the w3cvalidator.browser (.5 KB) file into the new directory, recompile and the application should now output valid XHTML.

Liked this post? Got a suggestion? Leave a comment