473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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="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!
Jul 20 '05 #1
11 1787


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/
Jul 20 '05 #2
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!
Jul 20 '05 #3


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/
Jul 20 '05 #4
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!
Jul 20 '05 #5


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!
Jul 20 '05 #6


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/
Jul 20 '05 #7


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!
Jul 20 '05 #8


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!
Jul 20 '05 #9
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!
Jul 20 '05 #10


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/
Jul 20 '05 #11

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!
Jul 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Hayden Kirk | last post by:
Hey guys I am about to write some back end software for my business. I want a system that I can create invoices, clients etc... I can do this no problem. However I want to write it in modules so...
4
by: Robert Secon | last post by:
Hi there, when using IE5+ my style sheets just work fine, but Mozilla does not agree with it and I canīt figure out which mistake it is. Usually the thing about it is that every row is displayed...
12
by: Don Huan | last post by:
Hi my job is to migrate our WinForms application to ASP.NET. This app was build very modular so every peace of code can be replaced by another "modul". There are 1 VS-solution with about 60...
26
by: I_AM_DON_AND_YOU? | last post by:
This is the scenario: I have a VB.Net project comprising of a few Forms. On Form1 I have more than 20 buttons. There is a very lenghty code written in click event of each and every button. Right...
4
by: Fred Nelson | last post by:
Hi: I have a VB.NET web application that has been running just fine for several months without any cascading style sheets. Suddenly several users have had problems with the layout of the...
4
by: drew197 | last post by:
I am a newbie. I am editing someone elses code to make it compatible with Firefox and Safari. In IE, when you click on the proper link, a block of text is shown in a nice paragraph form. But, in...
1
by: Kat | last post by:
I have a master page, with a reference to a style sheet in the header, as usual: <link href="Styles.css" type="text/css" rel="stylesheet" /> I have a content page, with a label on it: ...
1
by: Jan Danielsson | last post by:
Hello all, This is probably more of an apache question, but I'm guessing there will be other mod_python-beginners who are wondering the same thing. Let's say I have a web app called MyApp....
5
by: deve8ore | last post by:
Hello, I have a Word document called (), a named range that will open up a new Word doc dependent on what a user selects in Excel. Could someone please guide me on how to set up VBA code to...
2
by: deve8ore | last post by:
Hello, I have an Excel workbook with ~ 21 sheets in it. Let's say 7 are named "Red1", "Red2", "Red3", ect.... Another 7 are named for "Green1, 2, 3", ect... and the last 7 are named "Blue1, 2,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.