namespace problem

A

Andy Chambers

I'm trying to create a little test system for checking the content of
xml documents. I'd like to have files something like this....

<xunit fo="http://www.w3.org/1999/XSL/Format">
<test name="sample ok"
xpath="//fo:page-sequence[1]/@master-reference"
expected="A4-portrait"/>
<test name="sample fail"
xpath="/non/existent/path"
expected="sdlfd"/>
</xunit>

....then use xslt to run through each test and check whether the value
resulting from the xpath expression meets the value in the expected
attribute. I'm using the dyn:dynamic extension to dynamically
evaluate the xpath and that seems to be working fine. My problem
comes with namespaces. How can I get my stylesheet to know about any
namespaces one might include in the test file? Here's the snippet of
xslt that actually runs the test.

<xsl:template match="test">
<xsl:variable name="xpath" select="@xpath"/>
<xsl:variable name="actual" select="concat('',
dyn:evaluate($xpath))"/>
<xsl:variable name="expected" select="@expected"/>

<xsl:choose>
<xsl:when test="$actual != $expected">
<error test="{@name}"
expected="{$expected}"
actual="{$actual}"/>
</xsl:when>
<xsl:eek:therwise>
<ok test="{@name}"/>
</xsl:eek:therwise>
</xsl:choose>

</xsl:template>

If I add xmlns:fo="...." to the template, it works correctly. Is
there any way I can achieve this by extracting the namespace from the
xunit element and putting it into the template?
 
R

Richard Tobin

I'm trying to create a little test system for checking the content of
xml documents. I'd like to have files something like this....

<xunit fo="http://www.w3.org/1999/XSL/Format">

I assume this should be xmlns:fo=...
I'm using the dyn:dynamic extension to dynamically
evaluate the xpath and that seems to be working fine. My problem
comes with namespaces.

It appears that dyn:evaluate() uses the namespaces in scope at the
point of the call, so there doesn't seem to be any way to set them.

I suggest instead writing a stylesheet that transforms your tests into
a stylesheet that executes them. That has the added advantage of not
needing dynamic evaluation at all.

-- Richard
 
A

Andy Chambers

I assume this should be xmlns:fo=...

I suppose so.
I suggest instead writing a stylesheet that transforms your tests into
a stylesheet that executes them. That has the added advantage of not
needing dynamic evaluation at all.

Yeah I thought that might be the case. Didn't think about the other
advantage though. Thanks.
 
P

Philippe Poulard

Andy Chambers a écrit :
I'm trying to create a little test system for checking the content of

Why don't you try an existing one ?

Have a look here :
http://reflex.gforge.inria.fr/xunit.html
xml documents. I'd like to have files something like this....

<xunit fo="http://www.w3.org/1999/XSL/Format">
<test name="sample ok"
xpath="//fo:page-sequence[1]/@master-reference"
expected="A4-portrait"/>
<test name="sample fail"
xpath="/non/existent/path"
expected="sdlfd"/>
</xunit>

<!--first, let's use a convenient root element
for importing the modules to use ; the last
namespace URI doesn't refer to a module,
it's the ns URI expected for accessing inside
the XSLFO document-->
<xcl:active-sheet
xmlns:xcl="http://ns.inria.org/active-tags/xcl"
xmlns:io="http://ns.inria.org/active-tags/io"
xmlns:xunit="http://reflex.gforge.inria.fr/xunit.html"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<!--let's define our test case-->
<xunit:test-case name="test-case" label="My test case">
<!--parse the input XSLFO to test ;
it can be as well the output of an XSLT transformation
as a DOM tree or SAX events-->
<xcl:parse name="fo" source="file:///path/to/file.fo"/>
<!--assert that we have what is expected-->
<xunit:assert-string-equals result="A4-portrait" expected="{
string( $fo//fo:page-sequence[1]/@master-reference ) }"/>
<xunit:assert-string-equals result="an other value" expected="{
string( $fo//fo:an/fo:eek:ther/fo:path ) }"/>
</xunit:test-case>

<!--generate an XML report-->
<xunit:merge-reports name="Summary of XSLFO tests" source="{
io:file( '.' ) }"
output="{ io:file( 'my-error-report.xml' ) }"/>
<!--the last step could be transforming the XML report generated
in HTML ; if you want to use the stylesheet I used in the
XUnit tutorial, just let me know (I should add it to the
bundle in the next release of the engine)-->

</xcl:active-sheet>

If you already know XPath and XSLT, you should understand the above
script ; unlike XSLT it's a script that accept several instructions set,
like XSLT it's XPath centric ; there are other little variations, have a
look at the tutorials if you want to learn more

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top