Modular XML & XSL Sheets

J

Johnny Ooi

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
 
M

Martin Honnen

Johnny said:
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" />
 
J

Johnny Ooi

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
 
M

Martin Honnen

Johnny said:
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.
 
J

Johnny Ooi

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
 
J

Johnny Ooi

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
 
M

Martin Honnen

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.
 
J

Johnny Ooi

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
 
J

Johnny Ooi

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
 
J

Johnny Ooi

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?
 
M

Martin Honnen

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.
 
J

Johnny Ooi

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
 

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

Similar Threads

Google sheets 0
XSL node as string 1
XSL Grouping 0
XML Attribute and XSL 1
xsl landscape printing 6
Digesting a table with XSL 2.0 2
Using XML and XSL together 1
XML/XSL/XPATH how to get a element value 8

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top