Modular XML & XSL Sheets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Johnny Ooi

    Modular XML & XSL Sheets

    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="documen t('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="documen t('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!
  • Martin Honnen

    #2
    Re: Modular XML &amp; XSL Sheets



    Johnny Ooi wrote:
    [color=blue]
    > 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="documen t('file1.xml')/>
    >
    > This doesn't work.[/color]

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


    --

    Martin Honnen

    Comment

    • Johnny Ooi

      #3
      Re: Modular XML &amp; XSL Sheets

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

      <xsl:variable name="var" select="documen t('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!

      Comment

      • Martin Honnen

        #4
        Re: Modular XML &amp; XSL Sheets



        Johnny Ooi wrote:
        [color=blue]
        > can you tell me why, then, if I do this:
        >
        > <xsl:variable name="var" select="documen t('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.[/color]

        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

        Comment

        • Johnny Ooi

          #5
          Re: Modular XML &amp; XSL Sheets

          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!

          Comment

          • Johnny Ooi

            #6
            Re: Modular XML &amp; XSL Sheets



            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!

            Comment

            • Martin Honnen

              #7
              Re: Modular XML &amp; XSL Sheets



              Johnny Ooi wrote:

              [color=blue]
              > 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?[/color]

              The XSLT 1.0 specification at
              The World Wide Web Consortium (W3C) is an international community where Member organizations, a full-time staff, and the public work together to develop Web standards.

              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

              Comment

              • Johnny Ooi

                #8
                Re: Modular XML &amp; XSL Sheets



                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!

                Comment

                • Johnny Ooi

                  #9
                  Re: Modular XML &amp; XSL Sheets



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

                  <xsl:variable name="data" select="documen t($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!

                  Comment

                  • Johnny Ooi

                    #10
                    Re: Modular XML &amp; XSL Sheets

                    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!

                    Comment

                    • Martin Honnen

                      #11
                      Re: Modular XML &amp; XSL Sheets



                      Johnny Ooi wrote:

                      [color=blue]
                      > <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.[/color]

                      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

                      Comment

                      • Johnny Ooi

                        #12
                        Re: Modular XML &amp; XSL Sheets




                        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!

                        Comment

                        Working...