satsamsk satsamsk - 5 months ago 20
HTML Question

display self closing xml elements to html output

My requirement is to display some

xml
elements (which are a part of input
xml
) in an
html
page.

for eg:

<org:specs>
<org:wild animal="6" species="land"/>
<org:fish animal="7" species="water"/>
<org:bird animal="8" species="trees"/>
<org:mammal animal="9" species="land"/>
</org:specs>


I want this entire
xml
snippet to appear as it is in the
html
output page, retaining the indentation followed in the input
Anyone has any idea on how to implement it using
XSLT
or
Xquery
?

EDIT: Jan 2nd, 2012

I tried the first solution(given below). It works, but i lost the indentation.
Giving more details, the implementation in my case will be using
Xquery
. I will use Marklogic command
xdmp:xslt-eval(<Stylesheet>,<A-Xquery-function-retrieving-the-above-xml>)
. When I use the first solution, the resulting html page has no indentation

Answer

Depending on the browser, HTML doesn't preserve white space very well unless you start doing CSS, which isn't supported uniformly in browsers. I do the following, which preserves white space on display and in the character serialization:

xquery version "1.0-ml";

declare namespace org = "someorg";

let $xml := 
<org:specs>
  <org:wild animal="6" species="land"/>
  <org:fish animal="7" species="water"/>
  <org:bird animal="8" species="trees"/>
  <org:mammal animal="9" species="land"/>
</org:specs>


let $quote-options :=
<options xmlns="xdmp:quote">
  <method>xml</method>
  <indent>yes</indent>
  <indent-untyped>yes</indent-untyped>
</options>

return

<div><pre>
{xdmp:quote($xml, $quote-options)}
</pre></div>