Read a plain file from XSLT 1.0

Discussion in 'XML' started by, Apr 30, 2008.

  1. Guest

    , Apr 30, 2008
    1. Advertisements

  2. In article <>,
    <> wrote:

    >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:
    >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">

    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

    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
    Richard Tobin, May 1, 2008
    1. Advertisements

  3. 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.
    Joseph J. Kesselman, May 1, 2008
    1. Advertisements

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. Hannes Heckner
  2. pradeep gummi
    pradeep gummi
    Aug 13, 2003
  3. mahesh
    Real Gagnon
    Feb 17, 2007
  4. Jorge Luzarraga Castro

    Please Advice. What´s the right path? plain file vs xml file

    Jorge Luzarraga Castro, Sep 7, 2004, in forum: ASP .Net Web Services
    Denis Kondratyev
    Sep 8, 2004
  5. Alex Dowad
    Michel Demazure
    May 1, 2010

Share This Page