This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Using document() without a file
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] Using document() without a file
- From: Edmund Mitchell <EMitchell at fnf dot com>
- Date: Mon, 5 Feb 2001 06:59:46 -0800
- Reply-To: xsl-list at lists dot mulberrytech dot com
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)
stream.close()
return node
def registerSubst(self, uri, node):
self._cache[uri] = node
return
sheet_1 = """\
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match = "/">
<xsl:value-of select='name(/*)'/><xsl:text>
</xsl:text>
<xsl:value-of
select='name(document("http://www.fourthought.com/4Suite/4Suite.xsa")/*)'/><
xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>"""
source_1 = "<spam/>"
subst_1 = "<eggs/>"
#First what we'd normally have
processor = Processor()
processor.appendStylesheetString(sheet_1)
result = processor.runString(source_1)
print result
#Now let's substitute the 'http://www.fourthought.com/4Suite/4Suite.xsa'
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
sreader.registerSubst('http://www.fourthought.com/4Suite/4Suite.xsa',
node)
#So the reader we slipped in will be used throughout the processing,
#Including by the document() function
processor = Processor(reader=sreader)
processor.appendStylesheetString(sheet_1)
result = processor.runString(source_1)
print result
----------------------------------%------------------------------------
Here is the output I get from this
[uogbuji@borgia borrowed]$ python uo_20010112.py
<?xml version='1.0' encoding='UTF-8'?>
spam
xsa
<?xml version='1.0' encoding='UTF-8'?>
spam
eggs
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: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list