为什么我的XPath断言测试确实在eXist-db中传递?

时间:2016-03-17 08:41:10

标签: unit-testing xpath exist-db

我有一个测试函数,它以文档为参数,并将其从XML转换为HTML。为此,我想使用一些测试。在这种情况下,%test:assertXPath似乎是一个很好的候选人。但是,如果我使用整个路径,我无法理解它的行为。

我的功能:

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
};

结果:

<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>

很明显,唯一没有通过的断言是$result/html。为什么呢?

1 个答案:

答案 0 :(得分:2)

您从XPath断言中遗漏了命名空间。您生成的<html>元素位于http://www.w3.org/1999/xhtml命名空间中。

所以你需要将你的断言改为:

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

或者您需要使用declare namespace xhtml = "http://www.w3.org/1999/xhtml";在您的序言中声明名称空间前缀,然后您的断言将如下所示:

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