[MarkLogic Dev General] handling of empty elements

Alan Darnell alan.darnell at utoronto.ca
Fri Aug 31 10:40:06 PDT 2012

I suspect this is an old question answered elsewhere but I can't seem to find a complete description of best practices for handling empty HTML elements.  Say I'm outputting some text content from an XML document to the browser.  Typically, I'll wrap that content in a div or some other HTML element.  



If, however, it happens that the value of $content is empty, then the following is sent to my browser

<div />

which it interprets as an open div and my HTML formatting gets messed up.

I understand that if the div is associated with the xhtml namespace


ML will send back the following instead: 


which the browser can handle.

I also understand that I can declare a default element namespace of http://www.w3.org/1999/xhtml, but if the content I'm working with does not declare its own namespace, then it inherits this default namespace and can create problems when working with that content.

I can use a hack to ensure that there is always something between the div elements (e.g. <div>{$content}&nbsp;</div>)  or test that the value of $content is not empty before outputting the div, but that is too easy to forget to do and all those checks and placeholder values make the code look messy.

I know I can also associate the xhtml namespace with a specific prefix and use that with all my HTML elements (e.g. <h:div>{$content}</h:div> ), but it is too easy to forget to do this for every element.

Is there a better way to handle this?  It must be a common issue so I'm guessing there is a better approach than the options I've been able to figure out.  

Thanks in advance for any help.


More information about the General mailing list