csv export problem
Monday, 26 September 2011 · 4 messages
KC
Kevin S. Clarke26 Sep 2011 20:20 #
Hi,
Timely that CSV is being discussed. I have a question that has been stumping me...
I want to be able to output something like:
"text here", "<div><a href=""http://basex.org"">text, and
more</a></div>", "text here again"
What's stumping me is being able to double-quote escape the quotation marks that are in my markup. I've tried serializing my elements and then replacing the single quotes of the attributes to double quotes but then when I try to write that to a file (either serializing as text or xml) the tags are escaped to < etc. Here is a bit of what I've tried:
declare option output:indent 'no';
declare variable $txtSer :=
<serialization-parameters
xmlns="http://www.w3.org/2010/xslt-xquery-serialization">
<method value="text"/>
</serialization-parameters>;
declare variable $xmlSer :=
<serialization-parameters
xmlns="http://www.w3.org/2010/xslt-xquery-serialization">
<method value="xml"/>
<indent value="no"/>
</serialization-parameters>;
declare variable $outFile := 'out2.csv';
declare variable $xml := <sample><a href="http://basex.org">text, and
more text</a></sample>;
declare function local:writeEOL() {
file:append($outFile, '
', $txtSer)
};(:
Trying to output markup so I can eventually output something like: "text", "<div><a href=""http://basex.org"">text, and
more</a></div>", "text"
:)
(
file:append($outFile, $xml, $txtSer), (: only text of xml out :)
local:writeEOL(),
file:append($outFile, $xml, $xmlSer), (: xml out but can't escape quotes :)
local:writeEOL(),
file:append($outFile, serialize($xml), $txtSer), (: tags escaped :)
local:writeEOL(),
file:append($outFile, serialize($xml, $txtSer), $txtSer), (: only
text out :)
local:writeEOL(),
file:append($outFile, serialize($xml), $xmlSer), (: tags escaped :)
local:writeEOL(),
file:append($outFile, serialize($xml), $txtSer), (: tags escaped :)
local:writeEOL(),
file:append($outFile, concat('<![CDATA[', serialize($xml), ']]>'),
$xmlSer), (: tags escaped :)
local:writeEOL(),
file:append($outFile, concat('<![CDATA[', serialize($xml), ']]>'),
$txtSer), (: tags escaped :)
local:writeEOL(),
file:append($outFile, '<,<', $txtSer) (: both are escaped :)
)I don't seem to be able to output the markup in either text or xml mode. What am I doing wrong?
Thanks!
Kevin
KC
Kevin S. Clarke26 Sep 2011 20:23 #
Quoted text · Kevin S. Clarke · 3 lines
On Mon, Sep 26, 2011 at 12:19 PM, Kevin S. Clarke <ksclarke at gmail.com> wrote: > I don't seem to be able to output the markup in either text or xml > mode. What am I doing wrong?
Oops, that was incorrect... I can output the markup in xml mode, of course... I just can't double escape the quotes that way. I'd be good if I could output the xml using single quotes for the attributes <href='http://basex.org'> but single quotes in my XQuery are output using double ones.
Kevin
CG
Hi Kevin,
Quoted text · Kevin S. Clarke · 4 lines
> Oh, I think I may have found the answer... it seems I want the 'raw' > serializer instead of the text one. That will output <, >, etc. > without escaping it. I should be able to serialize the xml to string, > double-quote the single quotes, and then write to file using 'raw'...
yes, you were faster.. Those two examples should give you the expected result:
file:write('test1', '<x/>', map { 'format':='no' })
file:write('test2', '<x/>', map { 'method':='raw' })Both serialization parameters are BaseX-specific [1], as the official standard doesn't offer any help here..
Christian
[1] http://docs.basex.org/wiki/Serialization
KC
Oh, I think I may have found the answer... it seems I want the 'raw' serializer instead of the text one. That will output <, >, etc. without escaping it. I should be able to serialize the xml to string, double-quote the single quotes, and then write to file using 'raw'...
Kevin
Quoted text · Kevin S. Clarke · 14 lines
On Mon, Sep 26, 2011 at 12:22 PM, Kevin S. Clarke <ksclarke at gmail.com> wrote: > On Mon, Sep 26, 2011 at 12:19 PM, Kevin S. Clarke <ksclarke at gmail.com> wrote: > >> I don't seem to be able to output the markup in either text or xml >> mode. What am I doing wrong? > > Oops, that was incorrect... I can output the markup in xml mode, of > course... I just can't double escape the quotes that way. I'd be good > if I could output the xml using single quotes for the attributes > <href='http://basex.org'> but single quotes in my XQuery are output > using double ones. > > Kevin >
Written for BaseX 6.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery