RE: [xsl] Using document() without a file

Subject: RE: [xsl] Using document() without a file
From: Edmund Mitchell <EMitchell@xxxxxxx>
Date: Mon, 5 Feb 2001 06:59:46 -0800
Hello Stu
Here's a demo written by Uche Ogbuji using 4XSLT where you can not only use
a document from a string, you can switch documents around:


What if you have an XSLT source document in memory as a string, which
you don't wish to write to disk.  Yet you want to be able to refer to it
using the XSLT document() function, or some similar facility.

You can do this by substituting a default reader with your own.  See the
following code snippet for an example.

from Ft.Lib import Uri, cDomlette
from xml.xslt.Processor import Processor

class SubstituteReader(cDomlette.RawExpatReader):
    def __init__(self):
        self._cache = {}

    def fromUri(self, uri, base='', ownerDoc=None, stripElements=None):
        abs_uri = Uri.BASIC_RESOLVER.normalize(uri, base)[0]
        if self._cache.has_key(abs_uri):
            return self._cache[abs_uri]
        stream = Uri.BASIC_RESOLVER.resolve(uri, base)
        node = self.fromStream(stream, base, ownerDoc, stripElements)
        return node

    def registerSubst(self, uri, node):
        self._cache[uri] = node

sheet_1 = """\
<xsl:stylesheet xmlns:xsl="";

  <xsl:template match = "/">
    <xsl:value-of select='name(/*)'/><xsl:text>


source_1 = "<spam/>"

subst_1 = "<eggs/>"

#First what we'd normally have
processor = Processor()
result = processor.runString(source_1)
print result

#Now let's substitute the ''
URI with our local string
sreader = SubstituteReader()

#It inherits the function of a regular cDomlette reader
node = sreader.fromString(subst_1)
#But now we make the switch

#So the reader we slipped in will be used throughout the processing,
#Including by the document() function
processor = Processor(reader=sreader)
result = processor.runString(source_1)
print result

Here is the output I get from this

[uogbuji@borgia borrowed]$ python
<?xml version='1.0' encoding='UTF-8'?>

<?xml version='1.0' encoding='UTF-8'?>

I hope that helps

Edmund M


I understand how to use document() when I have a file that can be accessed
with a URI. Is there a way to include an XML document that is in the form of
a string or as a DOM -- i.e., has not been written to a file -- without
going through the process of saving it as a file first?

 XSL-List info and archive:

 XSL-List info and archive:

Current Thread