Read a plain file from XSLT 1.0

R

Richard Tobin

I'd like to read a one-line text file using an XSLT transformation.
I'm using libxslt, so I have to use XSLT 1.0.

This post:

http://www.stylusstudio.com/xsllist/200508/post50080.html

is promising, but I'm afraid I can't decipher it. Can anybody help?

Mike is suggesting that you create another, XML, file that includes
your text file as an entity.

Suppose hello.txt contains your text - "hello world" for example.
Create hello.xml containing

<!DOCTYPE foo [
<!ENTITY ent SYSTEM "hello.txt">
]>
<foo>&ent;</foo>

Then when you read foo.xml with the document function it will be as
if you had a file containing <foo>hello world</foo>. Of course,
you'll be in trouble if the file contains text that is ill-formed
XML.

As an extension to this idea, to save creating a separate file, you
could put an entity reference to the text file *in the xslt stylesheet
itself*, and use document("") to refer to the stylesheet. To do this
the name of the text file would have to be fixed.

-- Richard
 
J

Joseph J. Kesselman

The other solution is to find or create an extension function which will
read and return the text file's contents. The advantage of this approach
is that, since you're returning it as data, it doesn't have to be a
well-formed XML Document Fragment; it can contain unbalanced <, >, -,
and & characters.

Depending on the details of your processor -- and what your stylesheet
actually does with the data -- it might still have to respect XML 1.0's
limitations on the legal character set. Unless the extension also
implements some custom escaping solution like <my:char ucode="3095"/>.

But this involves carving a large path into dubiously-portable
solutions. A better answer might be to write a separate preprocessor
tool which reads that file and writes out a safely XMLified version,
dealing with all of these issues in some appropriate manner... and then
have your stylesheet read and process that XML.
 

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


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top