If the data is already in the form of a DOM, pass the DOM Document node (or a DOMSource object that wraps it).
 
But don't create a DOM specially - DOM input to Saxon is much less efficient than Saxon's native tree format. 
 
You can pass the raw XML text as the parameter value if you prefer, and then parse it from within the stylesheet using the saxon:parse() extension function.
 
Or you can parse it into a Saxon tree from your application code, using the method StaticQueryContext.buildDocument().
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Jaco Van Staden (J J)
Sent: 23 June 2006 09:40
To: saxon-help@lists.sourceforge.net
Subject: [saxon] Passing XML to Xsl as parameter

Hi Guys,

How do you pass an xml string into xsl and load it into a document so you can query it?

The code I have so far is:

public static void main(String[] args)

        {

                try

                {

                        File xsltFile = new File(stylesheet);

                        File xmlFile = new File(filename);

                        File defFile = new File(defFilename);

                       

                        StringWriter sw = new StringWriter();

                        StreamResult result = new StreamResult(sw);

                       

                        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");

                        SAXTransformerFactory saxTFactory = (SAXTransformerFactory) TransformerFactory.newInstance();

                        Transformer transformer = saxTFactory.newTransformer(new StreamSource(xsltFile));

                        // I would like to instantiate an object and send the instance through to the Xsl.

                        Document dom = null;

                        try

                        {

                                dom = DocumentHelper.parseText("<?xml version='1.0'?><root><r NM='Hello World'>Text12</r></root>");

                        }

                        catch (DocumentException e1)

                        {

                                e1.printStackTrace();

                        }

                       

                        //transformer.setParameter("InputXml", dom.getRootElement().selectSingleNode("//r"));

                        transformer.setParameter("InputXml", dom.asXML());

                       

                        try

                        {

                                transformer.transform(new StreamSource(defFile), result);

                        }

                        catch (TransformerException e)

                        {

                                e.printStackTrace();

                        }

                       

                        String xmlString = sw.toString();                      

                        System.out.println(sw.toString());

                       

                }

                catch (TransformerConfigurationException e)

                {

                        e.printStackTrace();

                }

                catch (TransformerFactoryConfigurationError e)

                {

                        e.printStackTrace();

                }

        }

With the Xsl looking like:

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

<xsl:stylesheet version="2.0"

        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

        xmlns:fo="http://www.w3.org/1999/XSL/Format">

        <xsl:param name="InputXml" select="''" />

        <xsl:variable name="DataXml" select="doc($InputXml)" />

        <xsl:template match="/">

                <html>

                        <head>

                                <title>TestApp</title>

                        </head>

                        <body>

                                <table>

                                        <tr>

                                                <td><xsl:value-of select="$DataXml" /></td>

                                        </tr>

                                </table>

                        </body>

                </html>

        </xsl:template>

</xsl:stylesheet>

 

Regards

Jaco

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at
http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~