0

I have a testing function which takes a document as its argument and transforms it from XML into HTML. For it, I would like to use some tests. %test:assertXPath seems to be a good candidate in this case. However, I can’t understand its behavior if I use the whole path.

My function:

xquery version "3.0";

module namespace cust = 'http://46.28.111.241:8081/exist/db/apps/myapp/modules/cust';

declare namespace tei  = 'http://www.tei-c.org/ns/1.0';
declare namespace test = 'http://exist-db.org/xquery/xqsuite';
declare
    %test:args('<TEI xmlns="http://www.tei-c.org/ns/1.0">
                    <text>
                        <body>
                            <div n="1">
                                <head>Heading</head>
                                <p>paragraph</p>
                            </div>
                        </body>
                    </text>
                </TEI>',
                '/db/apps/myapp/resources/xslt/style-web.xsl')
    %test:assertXPath('$result//@*')
    %test:assertXPath('$result//*')
    %test:assertXPath('$result//*[@class = "chapter"]')
    %test:assertXPath('$result/html')
function cust:transform($doc as element(), $styleSheet as xs:anyURI) as node() {
    let $styleSheet := doc($styleSheet)
    let $document := 
        (
            <book n='1'>{($doc//tei:div[@n='1'])[1]}</book>
        )
    let $finale := transform:transform($document, $styleSheet, ())
    return $finale
};

The result:

<testsuites>
    <testsuite package="http://46.28.111.241:8081/exist/db/apps/myapp/modules/cust"
        timestamp="2016-03-17T09:14:40.107+01:00" failures="1" pending="0" tests="1" time="PT0.449S">
        <testcase name="transform" class="cust:transform">
            <failure message="assertXPath failed." type="failure-error-code-1">$result/html</failure>
            <output>
                <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                        <title/>
                        <meta charset="UTF-8"/>
                    </head>
                    <body>
                        <div id="wrapper">
                            <section xmlns:epub="http://www.idpf.org/2007/ops" epub:type="chapter">
                                <h1 class="chapter">Heading</h1>
                                <p>paragraph</p>
                            </section>
                        </div>
                    </body>
                </html>
            </output>
        </testcase>
    </testsuite>
</testsuites>

It is apparent the only one assertion which does not pass is the $result/html. Why?

Honza Hejzl
  • 874
  • 8
  • 23

1 Answers1

3

Your missing the namespace from your XPath assertion. The <html> element you are producing is in the http://www.w3.org/1999/xhtml namespace.

So you need to change your assertion to either:

%test:assertXPath('$result/*:html')

or you need to declare the namespace prefix in your prolog using declare namespace xhtml = "http://www.w3.org/1999/xhtml"; and then your assertion would look like:

%test:assertXPath('$result/xhtml:html')
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
adamretter
  • 3,885
  • 2
  • 23
  • 43
  • Interesting. Right now I was closing another one answer regarding to namespaces and **default** namespaces. I will let you know. – Honza Hejzl Mar 17 '16 at 15:06
  • Option 1 fails, option 2 throws an error: ``. Am I missing something? – Honza Hejzl Mar 17 '16 at 15:12
  • You have forgotten to declare the namespace declaration your prolog. – adamretter Mar 17 '16 at 18:04
  • I thought the first option should work without the declaration, in the case of the second option, I have it there: `declare namespace xhtml = "http://www.w3.org/1999/xhtml";`. ? – Honza Hejzl Mar 17 '16 at 19:18
  • Still no progress. I would expect even the basic `%test:assertXPath('$result//*[local-name()="html" and namespace::*="http://www.w3.org/1999/xhtml"]')` could work without any additional declaration in the prolog. If I could provide next examples of the code, I will. At the moment, I don’t know how to do that in a reasonable way here. – Honza Hejzl Mar 18 '16 at 07:36
  • That looks like a strange use of the namespace axis. I would avoid that. – adamretter Mar 22 '16 at 16:27
  • At the moment, nothing works for me, hence these desperate attempts. – Honza Hejzl Mar 22 '16 at 17:30
  • `%test:assertXPath('$result//*[local-name(.) eq "html"])` works here – adamretter Mar 22 '16 at 19:04
  • Thanks a lot, however, it fails in my case: `$result//*[local-name(.) eq "html"]`. Unhappy. (I have also added the missing quotation mark.) – Honza Hejzl Mar 23 '16 at 07:17
  • Could I use something similar to test that? Still no success. – Honza Hejzl Apr 02 '16 at 17:16
  • @HonzaHejzl Maybe you could study the examples to see some that work: https://github.com/eXist-db/exist/tree/develop/test/src/xquery – adamretter Apr 05 '16 at 23:34
  • Thanks, this helped a lot! Now I see what is needed. Everything works except one tiny detail—I can’t use paths starting from root with predicates: `/ns:html/ns:head/ns:title` passes, `/ns:html/ns:head/ns:title[. = "Tested Bunny"]` fails. – Honza Hejzl Apr 06 '16 at 08:57