CDATA in xquery

Monday, 2 December 2013 · 3 messages
EA
Erol Akarsu2 Dec 2013 22:29 #
I would like to generate CDATA of one xmk construct like this. This does not calculate $1,$f2 and $f3 and insert their serialized version into features tag Can we do this in xquery?
let $allfs := <record>
                      <name>{$name}</name>
                      <features>
                        <![CDATA[
                        <h3>Features:</h3>
                        <br/>
                        <ul>
                          <li>{$f1}</li>
                          <li>{$f2}</li>
                          <li>{$f3}</li>
                        </ul>
                        ]]>
                      </features>
                  </record>
Erol Akarsu
LQ
Liam R E Quin2 Dec 2013 22:44 #
Quoted text · Erol Akarsu · 17 lines
On Mon, 2013-12-02 at 15:29 -0500, Erol Akarsu wrote: > I would like to generate CDATA of one xmk construct like this. [...] > let $allfs := <record> > <name>{$name}</name> > <features> > <![CDATA[ > <h3>Features:</h3> > <br/> > <ul> > <li>{$f1}</li> > <li>{$f2}</li> > <li>{$f3}</li> > </ul> > ]]> > </features> > </record>
Watch out that if $f1 (say) contains the string ]]> you'd be liable to a cdata injection attack.
Having said that, no, I'd probably write an e() function,
declare function my:e($name as xs:string, $content as xs:string)
as xs:string
{
  return concat("<", $name, ">", $content, "</", $name, ">")
}

and use my:e("ul",
  concat(my:e("li", $f1),
         my:e("li", $f2),
         my:e("li", $f3))
and so on. Which gives you slightly more checking.
The implementation will probably generate &lt; and &gt; rather than CDATA; XML says they're equivalent. In XQuery 3 there's a per-element serialization option for CDATA sections, http://www.w3.org/TR/xslt-xquery-serialization-30/#XML_CDATA-SECTION-ELEMENTS so if BaseX implements that you have a way to get closer to what you want, perhaps, e.g. for generating RSS. However, you'd still need to construct a string containing "<h1>" etc. Liam
-- Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/ Pictures from old books: http://fromoldbooks.org/ Ankh: irc.sorcery.net irc.gnome.org freenode/#xml
EA
Erol Akarsu2 Dec 2013 23:06 #
Liam, Thanks for help, Basex implements serialize function. This can do:
   let $params := <output:serialization-parameters xmlns:output="
http://www.w3.org/2010/xslt-xquery-serialization">
                      <output:omit-xml-declaration value="yes"/>
                    </output:serialization-parameters>

    let $sf :=  fn:concat(fn:serialize(<h3>Features:</h3>,$params),
                        fn:serialize(<br/>,$params),
                        fn:serialize(
                            <ul>
                              <li>{$f1}</li>
                              <li>{$f2}</li>
                              <li>{$f3}</li>
                            </ul>,
                        $params))
Quoted text · Erol Akarsu · 55 lines
On Mon, Dec 2, 2013 at 3:44 PM, Liam R E Quin <liam at w3.org> wrote: > On Mon, 2013-12-02 at 15:29 -0500, Erol Akarsu wrote: > > I would like to generate CDATA of one xmk construct like this. > [...] > > let $allfs := <record> > > <name>{$name}</name> > > <features> > > <![CDATA[ > > <h3>Features:</h3> > > <br/> > > <ul> > > <li>{$f1}</li> > > <li>{$f2}</li> > > <li>{$f3}</li> > > </ul> > > ]]> > > </features> > > </record> > > Watch out that if $f1 (say) contains the string ]]> you'd be liable to a > cdata injection attack. > > Having said that, no, I'd probably write an e() function, > declare function my:e($name as xs:string, $content as xs:string) > as xs:string > { > return concat("<", $name, ">", $content, "</", $name, ">") > } > > and use my:e("ul", > concat(my:e("li", $f1), > my:e("li", $f2), > my:e("li", $f3)) > and so on. Which gives you slightly more checking. > > The implementation will probably generate &lt; and &gt; rather than > CDATA; XML says they're equivalent. > > In XQuery 3 there's a per-element serialization option for CDATA > sections, > > http://www.w3.org/TR/xslt-xquery-serialization-30/#XML_CDATA-SECTION-ELEMENTS > so if BaseX implements that you have a way to get closer to what you > want, perhaps, e.g. for generating RSS. However, you'd still need to > construct a string containing "<h1>" etc. > > Liam > > > -- > Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/ > Pictures from old books: http://fromoldbooks.org/ > Ankh: irc.sorcery.net irc.gnome.org freenode/#xml > >

⚡Generated with XQuery