Serializing unescaped xml to string
Thursday, 5 December 2013 · 11 messages · last reply 16 Dec 2013
JT
Joe Templeman5 Dec 2013 06:00 #
Hi all,
Is there a way to output unescaped XML to a string for debugging purposes? In unittests we would like to output the XML returned when a test fails, here is my example:
declare %unit:test function elementinfo-test:test-element-info()
{
let $cases :=
<cases>
<case>
<comment>Failed input</comment>
<input>input</input>
<output>
<result> output </result>
</output>
</case>
</cases>
for $case in $cases/case
let $result := {some function which returns XML}
let $u := unit:assert(deep-equal($case/output, result),
$case/comment/text()
|| " Input: " || $case/input/text()
|| " Expected: " || fn:serialize($case/output)
|| " Got: " || fn:serialize($result))
return $u
};But when a test fails, I get escaped XML as the output. Is there any way to get this as actual XML?
Thanks,
Joe
LQ
Liam R E Quin5 Dec 2013 08:44 #
Quoted text · Joe Templeman · 6 lines
On Wed, 2013-12-04 at 19:59 -0800, Joe Templeman wrote: > Hi all, > > Is there a way to output unescaped XML to a string for debugging purposes? > In unittests we would like to output the XML returned when a test fails, > here is my example:
[...]
Quoted text · Joe Templeman · 6 lines
> || " Got: " || fn:serialize($result)) > return $u > }; > > But when a test fails, I get escaped XML as the output. Is there any way to > get this as actual XML?
( "Input: ", $case/input/text(),
" Expected: " , $case/output,
"Got:", $result)
|| " Got: " ||
as a sequence...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
CG
Hi Joe,
one solution is to use "text" as serialization method:
serialize("<a/>", { 'method': 'text'})As an alternative, you can specify in the query prolog (the beginning) of your query that all results are to be output as text:
declare option output:method "text";Hope this helps,
Christian
Quoted text · Joe Templeman · 41 lines
On Thu, Dec 5, 2013 at 4:59 AM, Joe Templeman <joe at inkling.com> wrote: > Hi all, > > Is there a way to output unescaped XML to a string for debugging purposes? > In unittests we would like to output the XML returned when a test fails, > here is my example: > > declare %unit:test function elementinfo-test:test-element-info() > { > let $cases := > <cases> > <case> > <comment>Failed input</comment> > <input>input</input> > <output> > <result> output </result> > </output> > </case> > </cases> > for $case in $cases/case > let $result := {some function which returns XML} > let $u := unit:assert(deep-equal($case/output, result), > $case/comment/text() > || " Input: " || $case/input/text() > || " Expected: " || fn:serialize($case/output) > || " Got: " || fn:serialize($result)) > return $u > }; > > But when a test fails, I get escaped XML as the output. Is there any way to > get this as actual XML? > > Thanks, > Joe > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
CG
Hi Joe,
Quoted text · Joe Templeman · 7 lines
> <testcase name="test-element-info" time="PT0.011S"> > <failure message="Failed input Input: input Expected: output Got: > {removed}" type="UNIT0001"/> > </testcase> > > What I would like is ".. Expected: <result>output</result> ..." since what > is being returned is XML.
This is syntactically not possible, because "<" cannot occur in attribute values. The following XML is not well-formed:
<x a="<"/>However, it’s possible to output the value of an attribute as string, and then skip the entity encoding:
declare option output:method "text";
let $expected := serialize(<a/>)
let $xml := <failure message="Expected: { $expected }"/>
return $xml/@message/string()Hope this helps,
Christian
JT
When I do { 'method': 'text'} I just get the text from the XML, not the XML itself:
<testcase name="test-element-info" time="PT0.011S">
<failure message="Failed input Input: input Expected: output Got:
{removed}" type="UNIT0001"/>
</testcase>What I would like is ".. Expected: <result>output</result> ..." since what is being returned is XML.
On Thu, Dec 5, 2013 at 3:02 AM, Christian Grün <christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 60 lines
> Hi Joe, > > one solution is to use "text" as serialization method: > > serialize("<a/>", { 'method': 'text'}) > > As an alternative, you can specify in the query prolog (the beginning) > of your query that all results are to be output as text: > > declare option output:method "text"; > > > Hope this helps, > Christian > ___________________________ > > On Thu, Dec 5, 2013 at 4:59 AM, Joe Templeman <joe at inkling.com> wrote: > > Hi all, > > > > Is there a way to output unescaped XML to a string for debugging > purposes? > > In unittests we would like to output the XML returned when a test fails, > > here is my example: > > > > declare %unit:test function elementinfo-test:test-element-info() > > { > > let $cases := > > <cases> > > <case> > > <comment>Failed input</comment> > > <input>input</input> > > <output> > > <result> output </result> > > </output> > > </case> > > </cases> > > for $case in $cases/case > > let $result := {some function which returns XML} > > let $u := unit:assert(deep-equal($case/output, result), > > $case/comment/text() > > || " Input: " || $case/input/text() > > || " Expected: " || fn:serialize($case/output) > > || " Got: " || fn:serialize($result)) > > return $u > > }; > > > > But when a test fails, I get escaped XML as the output. Is there any way > to > > get this as actual XML? > > > > Thanks, > > Joe > > > > > > _______________________________________________ > > BaseX-Talk mailing list > > BaseX-Talk at mailman.uni-konstanz.de > > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > > >
JT
Ah that makes sense. Is there no way then to return a message from a unittest which contains XML? The framework creates the <failure> blocks based on the result of assertions so I don't have control over what is displayed it seems.
On Thu, Dec 5, 2013 at 12:56 PM, Christian Grün
<christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 28 lines
> Hi Joe, > > > <testcase name="test-element-info" time="PT0.011S"> > > <failure message="Failed input Input: input Expected: output > Got: > > {removed}" type="UNIT0001"/> > > </testcase> > > > > What I would like is ".. Expected: <result>output</result> ..." since > what > > is being returned is XML. > > This is syntactically not possible, because "<" cannot occur in > attribute values. The following XML is not well-formed: > > <x a="<"/> > > However, it’s possible to output the value of an attribute as string, > and then skip the entity encoding: > > declare option output:method "text"; > let $expected := serialize(<a/>) > let $xml := <failure message="Expected: { $expected }"/> > return $xml/@message/string() > > Hope this helps, > Christian >
CG
Christian Grün6 Dec 2013 11:40 #
Quoted text · Joe Templeman · 4 lines
> Ah that makes sense. Is there no way then to return a message from a > unittest which contains XML? The framework creates the <failure> blocks > based on the result of assertions so I don't have control over what is > displayed it seems.
We could possibly extend the existing Unit Module with additional functions or function arguments. Could you please give an example on how the testing output, or the Unit function signatures, should ideally look like in your opinion?
Thanks,
Christian
WP
Hi,
I've done something similar (I think) in BaseX using the serialize() function (XPath 3.0). It was easy enough that I suspect I don't understand the requirement here. As Christian points out, XML doesn't allow you to represent XML directly in an attribute value; I used an element.
When using the text output method it comes out as XML (because it's already been serialized). When using the XML output method, it comes out escaped (as it should be).
Cheers, Wendell
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^
Quoted text · Joe Templeman · 46 lines
On Thu, Dec 5, 2013 at 4:04 PM, Joe Templeman <joe at inkling.com> wrote: > Ah that makes sense. Is there no way then to return a message from a > unittest which contains XML? The framework creates the <failure> blocks > based on the result of assertions so I don't have control over what is > displayed it seems. > > > > On Thu, Dec 5, 2013 at 12:56 PM, Christian Grün <christian.gruen at gmail.com> > wrote: >> >> Hi Joe, >> >> > <testcase name="test-element-info" time="PT0.011S"> >> > <failure message="Failed input Input: input Expected: output >> > Got: >> > {removed}" type="UNIT0001"/> >> > </testcase> >> > >> > What I would like is ".. Expected: <result>output</result> ..." since >> > what >> > is being returned is XML. >> >> This is syntactically not possible, because "<" cannot occur in >> attribute values. The following XML is not well-formed: >> >> <x a="<"/> >> >> However, it’s possible to output the value of an attribute as string, >> and then skip the entity encoding: >> >> declare option output:method "text"; >> let $expected := serialize(<a/>) >> let $xml := <failure message="Expected: { $expected }"/> >> return $xml/@message/string() >> >> Hope this helps, >> Christian > > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
CG
Hi Joe,
a little preview: I agree that the current output of the unit reporting functions is not as convenient as it could be. This is why we’ll soon extend the XML testcase output, and add some more functions like
unit:assert-equals() which will create a more helpful error output.
Best,
ChristianQuoted text · Joe Templeman · 41 lines
On Thu, Dec 5, 2013 at 4:59 AM, Joe Templeman <joe at inkling.com> wrote: > Hi all, > > Is there a way to output unescaped XML to a string for debugging purposes? > In unittests we would like to output the XML returned when a test fails, > here is my example: > > declare %unit:test function elementinfo-test:test-element-info() > { > let $cases := > <cases> > <case> > <comment>Failed input</comment> > <input>input</input> > <output> > <result> output </result> > </output> > </case> > </cases> > for $case in $cases/case > let $result := {some function which returns XML} > let $u := unit:assert(deep-equal($case/output, result), > $case/comment/text() > || " Input: " || $case/input/text() > || " Expected: " || fn:serialize($case/output) > || " Got: " || fn:serialize($result)) > return $u > }; > > But when a test fails, I get escaped XML as the output. Is there any way to > get this as actual XML? > > Thanks, > Joe > > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >
CG
Christian Grün15 Dec 2013 16:37 #
Feel free to check out the updated example in our Wiki [1] and the latest snapshot [2].
[1] http://docs.basex.org/wiki/Unit_Module
[2] http://files.basex.org/releases/latest/
Quoted text · Christian Grün · 55 lines
On Sat, Dec 14, 2013 at 5:35 PM, Christian Grün <christian.gruen at gmail.com> wrote: > Hi Joe, > > a little preview: I agree that the current output of the unit > reporting functions is not as convenient as it could be. This is why > we’ll soon extend the XML testcase output, and add some more functions > like unit:assert-equals() which will create a more helpful error > output. > > Best, > Christian > ___________________________ > > On Thu, Dec 5, 2013 at 4:59 AM, Joe Templeman <joe at inkling.com> wrote: >> Hi all, >> >> Is there a way to output unescaped XML to a string for debugging purposes? >> In unittests we would like to output the XML returned when a test fails, >> here is my example: >> >> declare %unit:test function elementinfo-test:test-element-info() >> { >> let $cases := >> <cases> >> <case> >> <comment>Failed input</comment> >> <input>input</input> >> <output> >> <result> output </result> >> </output> >> </case> >> </cases> >> for $case in $cases/case >> let $result := {some function which returns XML} >> let $u := unit:assert(deep-equal($case/output, result), >> $case/comment/text() >> || " Input: " || $case/input/text() >> || " Expected: " || fn:serialize($case/output) >> || " Got: " || fn:serialize($result)) >> return $u >> }; >> >> But when a test fails, I get escaped XML as the output. Is there any way to >> get this as actual XML? >> >> Thanks, >> Joe >> >> >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >>
JT
Joe Templeman16 Dec 2013 22:25 #
Nice! Thanks for updating that, it'll be a huge help
On Sun, Dec 15, 2013 at 7:37 AM, Christian Grün
<christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 65 lines
> Feel free to check out the updated example in our Wiki [1] and the > latest snapshot [2]. > > [1] http://docs.basex.org/wiki/Unit_Module > [2] http://files.basex.org/releases/latest/ > ___________________________ > > On Sat, Dec 14, 2013 at 5:35 PM, Christian Grün > <christian.gruen at gmail.com> wrote: > > Hi Joe, > > > > a little preview: I agree that the current output of the unit > > reporting functions is not as convenient as it could be. This is why > > we’ll soon extend the XML testcase output, and add some more functions > > like unit:assert-equals() which will create a more helpful error > > output. > > > > Best, > > Christian > > ___________________________ > > > > On Thu, Dec 5, 2013 at 4:59 AM, Joe Templeman <joe at inkling.com> wrote: > >> Hi all, > >> > >> Is there a way to output unescaped XML to a string for debugging > purposes? > >> In unittests we would like to output the XML returned when a test fails, > >> here is my example: > >> > >> declare %unit:test function elementinfo-test:test-element-info() > >> { > >> let $cases := > >> <cases> > >> <case> > >> <comment>Failed input</comment> > >> <input>input</input> > >> <output> > >> <result> output </result> > >> </output> > >> </case> > >> </cases> > >> for $case in $cases/case > >> let $result := {some function which returns XML} > >> let $u := unit:assert(deep-equal($case/output, result), > >> $case/comment/text() > >> || " Input: " || $case/input/text() > >> || " Expected: " || fn:serialize($case/output) > >> || " Got: " || fn:serialize($result)) > >> return $u > >> }; > >> > >> But when a test fails, I get escaped XML as the output. Is there any > way to > >> get this as actual XML? > >> > >> Thanks, > >> Joe > >> > >> > >> _______________________________________________ > >> BaseX-Talk mailing list > >> BaseX-Talk at mailman.uni-konstanz.de > >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > >> >
Written for BaseX 7.7. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery