Search code examples
xsltxquerymarklogic

Whitespace in xsl text transformation


A XSLT with a xsl:text containing a single (or multiple) whitespace(s) is not printing the whitespace(s) in MarkLogic 9.0-9. See the following example:

xquery version "1.0-ml";

let $doc := 
  <doc>
    <foo>foo</foo>
    <bar>bar</bar>
  </doc>
let $xsl :=
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
      <xsl:output method="text" omit-xml-declaration="yes" indent="no" />

      <xsl:template match="doc">
          <xsl:value-of select="foo"/>
          <xsl:text> </xsl:text>
          <xsl:value-of select="bar"/>
      </xsl:template>
  </xsl:stylesheet>

return xdmp:xslt-eval($xsl, $doc) = "foo bar"

This returns false. The result is "foobar". I actually expected "foo bar". I also tried with <xsl:text xml:space="preserve"> </xsl:text> but this does not work either.

As a workaround I currently use <xsl:value-of select="' '"/> which works fine but I am wondering if this is a bug? Using the same transformation and document in Saxon prints the whitespaces.


Solution

  • For standard XQuery you should get what you want with

    declare boundary-space preserve;
    

    in the query prolog, see https://www.w3.org/TR/xquery-31/#id-boundary-space-decls and https://www.w3.org/TR/xquery-31/#id-whitespace.

    Example is https://xqueryfiddle.liberty-development.net/eiQZDbq/4 doing

    declare boundary-space preserve;
    
    declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
    declare option output:method 'text';
    
    
    let $doc := 
      <doc>
        <foo>foo</foo>
        <bar>bar</bar>
      </doc>
    let $xsl :=
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      version="2.0">
          <xsl:output method="text" omit-xml-declaration="yes" indent="no" />
    
          <xsl:template match="doc">
              <xsl:value-of select="foo"/>
              <xsl:text> </xsl:text>
              <xsl:value-of select="bar"/>
          </xsl:template>
      </xsl:stylesheet>
    
    return transform(map { 'source-node' : $doc, 'stylesheet-node' : $xsl })?output 
    

    returning foo bar while https://xqueryfiddle.liberty-development.net/eiQZDbq/2 without that declaration returns foobar.

    I have not checked whether Marklogic supports that declaration or some proprietary similar way to change parsing treatment of whitespace in element constructors.