Modular XML & XSL Sheets

Discussion in 'XML' started by Johnny Ooi, Nov 9, 2004.

  1. Johnny Ooi

    Johnny Ooi Guest

    I have two XML documents I want to refer to in my XSL sheet, but I'm
    having problems accessing them. I want to access them both using the
    document() function (or something similar that allows me to read from
    files), but I've done this:

    <xsl:variable name="var" select="document('file1.xml')/>

    This doesn't work. I've also tried this (with xsl:param as well, BTW)

    <xsl:variable name="var">
    <xsl:copy-of select="document('file1.xml')"/>
    </xsl:variable>

    And this does give me the contents, but I can't do $var/* - I get some
    error about fragments.

    Is there a way I can do this? I need to be able to read in two XML
    documents and manipulate them together, you see.

    Regards

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 9, 2004
    #1
    1. Advertising

  2. Johnny Ooi wrote:

    > I have two XML documents I want to refer to in my XSL sheet, but I'm
    > having problems accessing them. I want to access them both using the
    > document() function (or something similar that allows me to read from
    > files), but I've done this:
    >
    > <xsl:variable name="var" select="document('file1.xml')/>
    >
    > This doesn't work.


    There is a syntax error, you need at least
    <xsl:variable name="var" select="document('file1.xml')" />
    but you usually want to access certain elements in that document e.g.
    <xsl:variable name="var" select="document('file1.xml')/root/element" />


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 9, 2004
    #2
    1. Advertising

  3. Johnny Ooi

    Johnny Ooi Guest

    Thanks for that, can you tell me why, then, if I do this:

    <xsl:variable name="var" select="document('Tree.xml')/Node"/>
    <xsl:copy-of select="$var"/>

    I don't get anything displayed?

    The structure of the file Tree.xml has a single node called "Node" as
    its root.

    Regards

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 9, 2004
    #3
  4. Johnny Ooi wrote:

    > can you tell me why, then, if I do this:
    >
    > <xsl:variable name="var" select="document('Tree.xml')/Node"/>
    > <xsl:copy-of select="$var"/>
    >
    > I don't get anything displayed?
    >
    > The structure of the file Tree.xml has a single node called "Node" as
    > its root.


    Wich XSLT processor are you using? Where are the file located? Have you
    tried with an absolute URL instead of a relative one e.g.
    document('http://example.com/dir/Tree.xml')
    With the relative URL of course the processor needs to resolve it using
    a base URL and unless you provide more details it is not possible to
    tell what goes wrong.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 9, 2004
    #4
  5. Johnny Ooi

    Johnny Ooi Guest

    Sorry if I didn't make it clearer. I'm using XMLSPY and I was trying to
    use the free engine that Altova have made available for downloading. I
    changed it back to the internal debugging engine and it seems to be
    correctly chucking out the XML. More surprisingly, I changed it _back_
    to the free engine and it seems to work also! >__< Man, I'm really
    having a bad day! Seems like computers always have it in for me, these
    days. :)

    On a more serious note, I think the problem is sorted now -- my machine
    was having a bad day. I'll keep working on it and let you know if I run
    into any more problems.

    Regards

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 9, 2004
    #5
  6. Johnny Ooi

    Johnny Ooi Guest

    Hi again, another question, related to the previous one, but probably
    more easier to answer. Is there a way I can say "load from file1.xml,
    but if you can't access it, dump the test 'Can't load from file1.xml' to
    the output tree in a comment" At the moment, the stuff is loading from
    the file, but if I change the filename, all I get is a message saying
    "can't find external entity", in other words, it can't read the file. I
    want to be able to catch this error (think Exceptions in Java) and
    handle the error myself. Is this possible?

    Regards

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 10, 2004
    #6
  7. Johnny Ooi wrote:


    > Is there a way I can say "load from file1.xml,
    > but if you can't access it, dump the test 'Can't load from file1.xml' to
    > the output tree in a comment" At the moment, the stuff is loading from
    > the file, but if I change the filename, all I get is a message saying
    > "can't find external entity", in other words, it can't read the file. I
    > want to be able to catch this error (think Exceptions in Java) and
    > handle the error myself. Is this possible?


    The XSLT 1.0 specification at
    http://www.w3.org/TR/xslt#document
    says "If there is an error retrieving the resource, then the XSLT
    processor may signal an error; if it does not signal an error, it must
    recover by returning an empty node-set. " thus if your XSLT processor
    continues processing you can check whether an empty nodeset has been
    returned.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 10, 2004
    #7
  8. Johnny Ooi

    Johnny Ooi Guest

    Hi, thanks again for the help, it turns out that if I step through the
    code using the debugger, I can catch the error and output appropriately,
    but if I let it go through, it will pop up with the error, not a big
    deal, but I was wondering.

    Next question now, I have put the code for reading from the file into a
    template and housed the call in a variable tag like this:

    <xsl:variable name="data">
    <xsl:call-template name="readData">
    <xsl:with-param name="file" select="$file"/>
    </xsl:call-template>
    </xsl:variable>

    Now, if I do

    <xsl:copy-of select="$data"/>

    I get the entire tree as previously discussed, however:

    <xsl:copy-of select="$data/somenode"/>

    Returns an error about a node fragment. Assume for the sake of this
    question, that somenode is the root node of the data tree in the file
    specified by the $file variable.


    Many thanks for your help

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 11, 2004
    #8
  9. Johnny Ooi

    Johnny Ooi Guest

    I have found a workaround, and that is do this:

    <xsl:variable name="data" select="document($file)"/>

    But that bypasses all the checks I want to do.

    Any suggestions?

    Regards

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 11, 2004
    #9
  10. Johnny Ooi

    Johnny Ooi Guest

    Oh, and on a side, note, is is possible to force output to be written to
    file specified by a variable in the XSL sheet?

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 13, 2004
    #10
  11. Johnny Ooi wrote:


    > <xsl:variable name="data">
    > <xsl:call-template name="readData">
    > <xsl:with-param name="file" select="$file"/>
    > </xsl:call-template>
    > </xsl:variable>
    >
    > Now, if I do
    >
    > <xsl:copy-of select="$data"/>
    >
    > I get the entire tree as previously discussed, however:
    >
    > <xsl:copy-of select="$data/somenode"/>
    >
    > Returns an error about a node fragment.


    XSLT 1.0 distinguishes so called node sets and so called result tree
    fragments. While you can apply all sorts of XPath expressions to node
    sets you can't do the same with result tree fragments constructed in a
    variable.
    Most XSLT processors however provide an extension function to convert a
    result tree fragment to a nodeset.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 13, 2004
    #11
  12. Johnny Ooi

    Johnny Ooi Guest

    Hi, thanks for the info, but I want to ensure that this sheet will work
    for all processors, so I want to avoid using processor-specific
    functions where possible, and I can't find any generic functions that
    will do this. Do you know of any?

    Johnny

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Johnny Ooi, Nov 13, 2004
    #12
    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. Jim Lewis

    procedures vs. modular design?

    Jim Lewis, Nov 30, 2004, in forum: VHDL
    Replies:
    3
    Views:
    524
    Guenter Wolpert
    Dec 1, 2004
  2. Tohid

    Modular Problem!

    Tohid, Oct 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    351
    Tohid
    Oct 22, 2003
  3. Murl Brown

    Creating a dynamic modular site...

    Murl Brown, Sep 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    317
  4. Replies:
    1
    Views:
    3,691
    A. Bolmarcich
    May 27, 2005
  5. Carl Corcoran
    Replies:
    1
    Views:
    291
    Bob Barrows
    Nov 12, 2003
Loading...

Share This Page