[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Using document() without a file

Subject: RE: Using document() without a file
From: Edmund Mitchell <EMitchell@xxxxxxx>
Date: Mon, 5 Feb 2001 06:59:46 -0800
self processing xslt document
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>&#10;</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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.