[MarkLogic Dev General] handling of empty elements

Justin Makeig Justin.Makeig at marklogic.com
Fri Aug 31 10:54:47 PDT 2012


The output option is your friend here
<http://www.w3.org/TR/xslt-xquery-serialization/#html-output>. 

declare option xdmp:output "method=html"; (: Tell the serializer that you want HTML :)
xdmp:set-response-content-type("text/html"), (: Always send text/html to browsers. :)
'<!DOCTYPE html>', (: HTML5 doctype to trigger standards-based rendering :)
<html lang="en"> {(: Look ma, no namespaces. :)}
<head>
<meta charset="utf-8" />
<title>Untitled</title>
<script type="text/javascript">
 if(1 &lt; 2 &amp;&amp; 3 &lt; 4) {{
   alert("&amp;");
 }}
</script>
</head>
<body>
  <h2>Empty textarea</h2>
  <textarea></textarea>
  <h2>Empty div</h2>
  <div></div>
</body>
</html>


Justin







On Aug 31, 2012, at 10:40 AM, Alan Darnell wrote:

> 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.  
> 
> e.g.
> 
> <div>{$content}</div>
> 
> 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
> 
> http://www.w3.org/1999/xhtml
> 
> ML will send back the following instead: 
> 
> <div></div>
> 
> 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.
> 
> Alan
> 
> _______________________________________________
> General mailing list
> General at developer.marklogic.com
> http://developer.marklogic.com/mailman/listinfo/general

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4375 bytes
Desc: not available
Url : http://developer.marklogic.com/pipermail/general/attachments/20120831/6b858874/attachment-0001.bin 


More information about the General mailing list