0

I’m having a small issue with extra spaces in the output of typeswitch (eXist 5.3) that I don’t know how to solve. Given this small example:

xquery version "3.1";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare function local:test($nodes as node()*)
{
    for $node in $nodes
    return
        typeswitch ($node)
        case element (test) return
            local:test($node/(node()|text()))
        case element (tei:seg) return
            local:test($node/(node()|text()))
        case element (tei:num) return
            local:test($node/(node()|text()))
        case element (tei:w) return
            local:test($node/(node()|text()))
        case element (tei:gap) return
            "[...]"
        case text() return
            $node/string()
        default return
            local:test($node/(node()|text()))
};

let $node :=
<test>
<seg xmlns="http://www.tei-c.org/ns/1.0" n="1" type="unite" xml:id="cat_agr_s1_u1">
   <seg type="title" resp="#apocryphe">
      <num value="1"> I</num>. <w xml:id="w_cato_agr_2" lemma="quomodo">Quomodo</w>
      <w xml:id="w_cato_agr_3" lemma="ager">agrum</w>
      <w xml:id="w_cato_agr_4" lemma="emo">emi</w> ,
      <w xml:id="w_cato_agr_5" lemma="pararique">pararique</w>
      <w xml:id="w_cato_agr_6" lemma="oporteo">oporteat</w>. 
   </seg>
   <gap reason="editorial" unit="word" quantity="182"> </gap>
</seg>
</test>
return
    <p>{local:test($node)}</p>

I get this output:

<p> I .  Quomodo agrum emi  ,
               pararique oporteat . 
            [...]</p>

But what I am looking for is the following - where the punctutation is not separated by a space from the text:

<p> I. Quomodo agrum emi, pararique oporteat. [...]</p>

Bear in mind that in production the content inside <p> may be mixed in final html output.

jbrehr
  • 775
  • 6
  • 19
  • 1
    The extra whitespace is because of the final return where you are enclosing a sequence of strings in `

    ` rather than a single string. The challenge with your expected output is different treatment of whitespace. It appears you want to omit all newlines while preserving other whitespace in the original content?

    – David Denenberg Jul 11 '21 at 13:46
  • Indeed, so adding a declaration `declare boundary-space preserve;`got me what I needed. – jbrehr Jul 11 '21 at 13:51
  • Does setting the boundary space really avoid the space after a punctuation? I understand David's comment as a hint to use `case text() return $node` instead of `case text() return $node/string()`, as the latter outputs a sequence of strings in the end where a space is inserted. – Martin Honnen Jul 12 '21 at 13:43
  • As it goes I needed both `declare boundary-space preserve;` and changing the return to `$node` only to get what I finally needed with the output. The latter was a suggestion on a Slack that I ended up integrating. – jbrehr Jul 12 '21 at 17:08

0 Answers0