[XML-SIG] 4Suite problems

Uche Ogbuji uche.ogbuji@fourthought.com
Fri, 13 Dec 2002 13:50:41 -0700


> On Fri, Dec 13, 2002 at 08:41:59AM -0700, Uche Ogbuji wrote:
> > > def ProcessFile(input, output):
> > >         XSLT = open("ujds.xslt", "r").read()
> > >         XML = open(input, "r").read()
> > >         xsltprocessor = Processor()
> > >         transform = InputSource.DefaultFactory.fromString(XSLT)
> > >         document = InputSource.DefaultFactory.fromString(XML)
> > I always strongly suggest that you still use URIs here.  For example
> > transform = InputSource.DefaultFactory.fromString(XSLT, "ujds.xslt")
> > This avoids errors if you try to do something later that performs resolution 
> > against a relative URI.
> > Note that you can just write:
> >         xsltprocessor = Processor()
> >         transform = InputSource.DefaultFactory.fromUri("ujds.xslt")
> >         document = InputSource.DefaultFactory.fromUri(input)
> > Which will eliminate the error and will have the additional advantage of being 
> > a tad faster.
> 
> Thanks, it works now!
> 
> I have one more thing though - As I already have the XML file parsed to
> a DOM instance, it would be a nice thing if I could use that DOM
> instance instead of reading the whole XML file again from disk. I have
> tried this approach:
> 
> def ProcessFile(dom, input, output):
> 
>         xsltprocessor = Processor()
>         
>         transform = InputSource.DefaultFactory.fromUri("ujds.xslt")
> 
>         xsltprocessor.appendStylesheet(transform)
>         result = xsltprocessor.runNode(dom, input)
>         if result:
>                 open(output, "w").write(result)
> 
> Which results in a file with this line only:
> 
> <?xml version='1.0' encoding='UTF-8'?>
> 
> I am trying to output as HTML, which now works nicely through the other
> way. How can I (if possible/feasible at all) just use the DOM instance
> instead of loading the file?

Hmm.  Works for me.  Try this script and tell me what you get:

TRANSFORM = """<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
"""

SOURCE = """<spam id="eggs">I don't like spam</spam>"""

from Ft.Xml.Xslt import Processor
processor = Processor.Processor()
from Ft.Xml.Domlette import NonvalidatingReader
#Create a DOM for the transform
transform = NonvalidatingReader.parseString(TRANSFORM, 
"http://spam.com/identity.xslt")
#Create a DOM for the source document
source = NonvalidatingReader.parseString(SOURCE, "http://spam.com/doc.xml")
processor.appendStylesheetNode(transform, "http://spam.com/identity.xslt")
result = processor.runNode(source, "http://spam.com/doc.xml")
print result


I get:

$ python domxslt.py
<?xml version='1.0' encoding='UTF-8'?>
<spam id='eggs'>I don't like spam</spam>


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Tour of 4Suite - http://www.xml.com/pub/a/2002/10/16/py-xml.html
Proper XML Output in Python - http://www.xml.com/pub/a/2002/11/13/py-xml.html
RSS for Python - http://www-106.ibm.com/developerworks/webservices/library/ws-p
yth11.html
Debug XSLT on the fly - http://www-106.ibm.com/developerworks/xml/library/x-deb
ugxs.html