为什么我的 XPath 断言测试在 eXist-db 中通过? [英] Why my XPath assertion test does pass in eXist-db?

查看:23
本文介绍了为什么我的 XPath 断言测试在 eXist-db 中通过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的功能:

xquery 版本3.0";模块命名空间 cust = 'http://46.28.111.241:8081/exist/db/apps/myapp/modules/cust';声明命名空间 tei = 'http://www.tei-c.org/ns/1.0';声明命名空间测试 = 'http://exist-db.org/xquery/xqsuite';宣布%test:args('<TEI xmlns="http://www.tei-c.org/ns/1.0"><文本><身体><div n="1"><head>标题</head><p>段落</p>

</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() {让 $styleSheet := doc($styleSheet)让 $document :=(<book n='1'>{($doc//tei:div[@n='1'])[1]}</book>)让 $finale := transform:transform($document, $styleSheet, ())返回 $finale};

结果:

<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 失败."type="failure-error-code-1">$result/html</failure><输出><html xmlns="http://www.w3.org/1999/xhtml"><头><标题/><meta charset="UTF-8"/><身体><div id="包装器"><部分 xmlns:epub="http://www.idpf.org/2007/ops" epub:type="chapter"><h1 class="chapter">标题</h1><p>段落</p></节>

</html></测试用例></testsuite></testsuites>

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

解决方案

您的 XPath 断言中缺少命名空间.您正在生成的 元素位于 http://www.w3.org/1999/xhtml 命名空间中.

因此您需要将断言更改为:

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

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

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

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?

解决方案

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

这篇关于为什么我的 XPath 断言测试在 eXist-db 中通过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆