namespace problem

Discussion in 'XML' started by Andy Chambers, Feb 11, 2008.

  1. 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?

    --
    Andy
     
    Andy Chambers, Feb 11, 2008
    #1
    1. Advertising

  2. In article <>,
    Andy Chambers <> wrote:
    >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
    --
    :wq
     
    Richard Tobin, Feb 11, 2008
    #2
    1. Advertising

  3. On Feb 11, 1:54 pm, (Richard Tobin) wrote:
    > In article <>,
    > Andy Chambers <> wrote:
    >
    > >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 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.

    --
    Andy
     
    Andy Chambers, Feb 11, 2008
    #3
  4. 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 !
     
    Philippe Poulard, Feb 11, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    573
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,136
  3. Anonymous
    Replies:
    3
    Views:
    534
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    452
    Woebegone
    Nov 19, 2004
  5. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,368
    Howard
    Nov 29, 2004
Loading...

Share This Page