wordpress hit counter
OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

Why is this illegal xhtml?

  • rated by 0 users
  • This post has 4 Replies |
  • 0 Followers
  • I'm upgrading a tool for generating xslt which produces xhtml. I have a problem that some kind person may shead light on, even if this is the wrong place to ask.

    Why in xhtml1.0 is it illegal to use the <span> element at the top level?

    Example

    <html> <body>

    <span class="gray">The sky is gray</span><span class="blue"> and I am blue.</span>

    </body></html>

    The w3.org html validator indicates this is not valid, and that the valid tags are div, h1, p and so on.

    Note that div which is the block level equal of span breaks the line, which is not acceptable.

    Yes it is true, I could wrap the span elements in a div, but that breaks the structure and and I'm trying create several types of documents (pdf, rtf etc) with the same structure.

    Can anyone help me understand why span would not be legal in the above contex?

    Thanks,

     

  • The <span> is not legal in that context because strict XHTML requires that the content within the BODY all be inside paragraphs, headings, or divs.  Here's a version of your document that renders the same result (no line break) and does validate as valid strict XHTML 1.0 at http://validator.w3.org:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head><title>This is the title</title></head>
    <body>
    <p><span class="gray">The sky is gray</span><span class="blue"> and I am blue.</span></p>
    </body>
    </html>

    Note that I changed three things to get this to validate:

    • added a doctype for "XHTML 1.0 Strict"
    • added a head section with a title clause (as required by XHTML)
    • added <p></p> tags surrounding your spans inside the <body>

    If you want to avoid <p> because it adds some extra margin/padding around your text, keep in mind that you can style the paragraph with CSS to avoid that.  (For example, <p style="margin: 0">).  Or you can put <div> tags around your content instead of <p>; i.e., put them around the whole thing, including both spans.  That, too, will validate as XHTML 1.0 Strict.

    Hope that helps,
    Doug

    - Doug Mahugh Technical Evangelist, Microsoft
  • Thanks for the reply!
    Ok, I understand that inline elements, and span is classified as inline, are not allowed at the top level. But why? and why is the following legal? Why would ins and del be classified as block level when they do not appear to define a block of text, which I assume has much to do with defining margins and box that the contents are displayed?
    Thanks!
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "<http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>">
    <html><head><title>span</title>
    <style type="text/css">
    .blue { color=#0000a0;}
    .gray { color=#808080;}
    </style> </head>
    <body>
    <ins class="gray">The day is gray</ins>
    <del class="blue"> and I'm blue.</del></body></html>
    
    
  • descdata:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "<http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>">
    
    I would ditch the angle brackets around the URL for the strict DTD too.
    That's technically not part of the format.
    Dennis E. Hamilton AIIM DMware Technical Coordinator http://odma.info http://DMware.info
  • But I <em>like</em> the brackets :-))

    I'm going to repost, and see if I can get my lines to wrap.

    Ok, I understand that inline elements, and span is classified as inline, are not allowed at the top level. But why? and why is the following legal? Why would ins and del be classified as block level when they do not appear to define a block of text, which I assume has much to do with defining margins and box that the contents are displayed?
    Thanks!
    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "<http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>">
    <html><head><title>span</title>
    <style type="text/css">
    .blue { color=#0000a0;}
    .gray { color=#808080;}
    </style> </head>
    <body>
    <ins class="gray">The day is gray</ins>
    <del class="blue"> and I'm blue.</del></body></html>

Page 1 of 1 (5 items)