[MarkLogic Dev General] handling of empty elements

Danny Sokolsky Danny.Sokolsky at marklogic.com
Fri Aug 31 13:46:39 PDT 2012


Hi Alan,

It should just be needed in the main module, as it calls the shots for serialization.

-Danny

-----Original Message-----
From: general-bounces at developer.marklogic.com [mailto:general-bounces at developer.marklogic.com] On Behalf Of Alan Darnell
Sent: Friday, August 31, 2012 12:32 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] handling of empty elements

Thanks for this Justin.  Should the xdmp:output option be set also in library modules or is it only necessary in main modules?  

Alan


On Aug 31, 2012, at 1:54 PM, Justin Makeig wrote:

> 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
> 
> <smime.p7s><ATT00001.c>

_______________________________________________
General mailing list
General at developer.marklogic.com
http://developer.marklogic.com/mailman/listinfo/general


More information about the General mailing list