Project

Profile

Help

Passing XML as a String Parameter

Added by Anonymous over 18 years ago

Legacy ID: #3417890 Legacy Poster: packley (packley)

I would like to pass XML from my Java application to my Stylesheet (SAXON 8) as a parameter. Then in my stylesheet I'd like to be able to access this XML using XPATH. How can I do this? Java: transformer.setParameter("ALT_XML_DOCUMENT_1",myXMLString); XSL: <xsl:param name="ALT_XML_DOCUMENT_1"/> .... <xsl:value-of select="$ALT_XML_DOCUMENT/Data/Person[@id='0000']/Name" /> I've attempted using exsl:node-set()... no luck. I don't want to read this XML from a file as that would add extra file io to this application... Any ideas? Thanks! Paul


Replies (2)

RE: Passing XML as a String Parameter - Added by Anonymous over 18 years ago

Legacy ID: #3418072 Legacy Poster: Michael Kay (mhkay)

The XML needs to be parsed. There are various ways of doing this. The simplest is probably to pass the parameter as a StreamSource: String xml = "<x>the xml</x>"; StringReader sr = new StringReader(xml); StreamSource ss = new StreamSource(sr); transformer.setParameter(name, ss); Alternatively you can use saxon:parse() to parse the XML once it arrives in the stylesheet.

RE: Passing XML as a String Parameter - Added by Anonymous over 18 years ago

Legacy ID: #3418346 Legacy Poster: packley (packley)

Yes!!! Your suggestion worked perfectly. By the way, I tried using the saxon:parse() function. It always threw an exception when I accessed the variable which I assigned to the results of the saxon:parse() call. If you are interested I have copied a partial stack trace below. In any event, your suggestion works perfectly so I'm all set now. Thanks! Paul org.xml.sax.SAXParseException: Document root element is missing. at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3376) at org.apache.crimson.parser.Parser2.fatal(Parser2.java:3364) at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:668) at org.apache.crimson.parser.Parser2.parse(Parser2.java:337) at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448) at net.sf.saxon.event.Sender.sendSAXSource(Sender.java:274) at net.sf.saxon.event.Sender.send(Sender.java:119) at net.sf.saxon.event.Sender.send(Sender.java:46) at net.sf.saxon.functions.Parse.evaluateItem(Parse.java:59) at net.sf.saxon.instruct.SimpleContentConstructor.evaluateItem(SimpleContentConstructor.java:177) at net.sf.saxon.expr.Atomizer.evaluateItem(Atomizer.java:112) at net.sf.saxon.instruct.ValueOf.processLeavingTail(ValueOf.java:163) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:162) at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:324) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:162) at net.sf.saxon.instruct.Block.processLeavingTail(Block.java:324) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:162) at net.sf.saxon.instruct.Template.expand(Template.java:95) at net.sf.saxon.instruct.Template.processLeavingTail(Template.java:79) at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:290) at net.sf.saxon.instruct.ApplyTemplates.defaultAction(ApplyTemplates.java:325) at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:283) at net.sf.saxon.Controller.transformDocument(Controller.java:1406) at net.sf.saxon.Controller.transform(Controller.java:1234) at com.qwest.xml.xt.XT.transformation(XT.java:159) at com.qwest.xml.xt.XT.doTransformation(XT.java:116) at com.qwest.xml.xt.XT.main(XT.java:193) --------- org.xml.sax.SAXParseException: Document root element is missing.

    (1-2/2)

    Please register to reply