XIncludeFilter Help
Added by Anonymous over 18 years ago
Legacy ID: #3845813 Legacy Poster: Kranklin (kranklin)
Hi, I am writing a server that uses XSLT to present xml Documents. I Would like to be able to use XInclude, however i cant seem to figure out how to implement the XIncludeFilter and i dont know where to turn. Here is my server code (coldfusion - script), it shouldnt be hard to figure out: ------------------------------------- /// CreateObject("java", "class") returns the Static Class.... Running .init([args]) on it returns the Constructor of it. xmlInput = expandURL(attributes.xml); /// Attributes.xml is a url to an xml File, expanding it just adds ths http://websitename.com part // dbfactory = createObject("java", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl "); // Xerces dbfactory = createObject("java", "net.sf.saxon.dom.DocumentBuilderFactoryImpl"); // Saxon dbfactory.setNamespaceAware(true); /if(attributes.XInclude){ Removed because this was Xerces Only. dbfactory.setXIncludeAware(true); dbfactory.setFeature("http://apache.org/xml/features/xinclude", true); //dbfactory.setFeature(" http://apache.org/xml/features/xinclude/fixup-base-uris", false); }/ parser = dbfactory.newDocumentBuilder(); document = parser.parse(xmlInput); ------------------------ I was using Xerces to do it, but i ran into a problem with it, so now im just sticking with Saxon. Will appreciate any help
Replies (9)
Please register to reply
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3845827 Legacy Poster: Michael Kay (mhkay)
Do you actually need to build a DOM? (You only need it if your application actually uses the DOM for some purpose other than the XSLT transformation. Otherwise, it's better to let Saxon build its own tree in its own format.) If you do need to build a DOM, build it using Xerces, and then supply the document node wrapped in a DOMSource as input to Saxon. If you don't need to build a DOM, then it's better to supply a SAXSource as input to Saxon. There's a sample application included in TraxExamples.java that shows how to insert a SAX filter into the input pipeline. This can of course be an XInclude processor implemented as a SAX filter.
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3846588 Legacy Poster: Kranklin (kranklin)
I have been able to make alot of headway, using what i thought was right fromt he traxexamples.java... However, according to the example, the function: templates.newTaransformer([Source Here]) returns null, but tFactory.newTransformer([Source Here]) works... Here is my coldfusion(script) that have now: ---------------------------------------------- // SET XML and XSL Inputs xml = "http://new.a2zinventory.com/xml/home.xml"; xmlInput = createObject("java", "org.xml.sax.InputSource"); xmlInput.init(xml); xmlSource = createObject("java", "javax.xml.transform.sax.SAXSource"); xmlSource.init(xmlInput); xsl = "http://new.a2zinventory.com/xsl/default.xsl.cfm"; xslSource = createObject("java", "javax.xml.transform.stream.StreamSource"); //createObject("java", "org.xml.sax.InputSource"); xslSource.init(xsl); // USED TO STORE RESULT xmlWriter = createObject("java", "java.io.StringWriter"); xmlWriter = xmlWriter.init(); xmlResult = createObject("java", "javax.xml.transform.stream.StreamResult"); xmlResult = xmlResult.init(xmlWriter); // Transform tfactory = createObject("java", "net.sf.saxon.TransformerFactoryImpl"); // Saxon // Create a Templates Content Handler to handle parsing of the stylesheet templatesHandler = tfactory.newTemplatesHandler(); //Create an XMLReader and set its features. spfactory = createObject("java", "javax.xml.parsers.SAXParserFactory"); spfactory = spfactory.newInstance(); reader = spfactory.newSAXParser(); reader = reader.getXMLReader(); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); //Create an XMLFilter that modifies the stylehseet filter = createObject("java", "com.elharo.xml.xinclude.XIncludeFilter"); filter.init(); filter.setParent(reader); filter.parse(xmlInput); // Get the Templates object (generated during the parsing of the stylesheet) from the TemplatesHandler. templates = templatesHandler.getTemplates(); // do the transformation transformer = tFactory.newTransformer(xslSource); //--------------------------------------- This One works but does not perform the filter. //transformer = templates.newTaransformer(xmlSource); //------------------------------------------- Error occurs Here: the functions seems to be returning null. transformer.transform(xmlSource, xmlResult);
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3846596 Legacy Poster: Kranklin (kranklin)
Oh, and here was the orginal code, slightly modified: TransformerFactory tfactory = TransformerFactory.newInstance(); // If so, we can safely cast. SAXTransformerFactory stfactory = ((SAXTransformerFactory) tfactory); // Create a Templates ContentHandler to handle parsing of the // stylesheet. javax.xml.transform.sax.TemplatesHandler templatesHandler = stfactory.newTemplatesHandler(); // Create an XMLReader and set its features. XMLReader reader = makeXMLReader(); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); // Create a XMLFilter that modifies the stylesheet XMLFilter filter = new ModifyStylesheetFilter(); filter.setParent(reader); filter.setContentHandler(templatesHandler); // Parse the stylesheet. filter.parse(new InputSource(xslID)); // Get the Templates object (generated during the parsing of the stylesheet) // from the TemplatesHandler. Templates templates = templatesHandler.getTemplates(); // do the transformation templates.newTransformer().transform( new StreamSource(sourceID), new StreamResult(System.out)); ---> ---------------- I wasnt able to do the cast on my code, but i dont think that is the problem.
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3846652 Legacy Poster: Michael Kay (mhkay)
Is there a question here?
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3846845 Legacy Poster: Kranklin (kranklin)
Yes, my question is there any idea why templates.newTransformer(...) return null? Is it possible that i dont have all the required jars... or the classpath is missing something?
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3846873 Legacy Poster: Michael Kay (mhkay)
The Saxon implementation of Templates.newTransformer() (in class PreparedStylesheet) will never return null.
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3846885 Legacy Poster: Kranklin (kranklin)
I made a mistake: templates.newTaransformer(...); is not the problem. templates = templatesHandler.getTemplates(); <- That returns null so summed up: tfactory = createObject("java", "net.sf.saxon.TransformerFactoryImpl"); // Saxon // Create a Templates Content Handler to handle parsing of the stylesheet templatesHandler = tfactory.newTemplatesHandler(); templatesHandler.getTemplates() // this returns null So again, what could cause templatesHandler.getTemplates() that to return null? Is it possible there is an error with my XML/XSL and its not being output?
RE: XIncludeFilter Help - Added by Anonymous over 18 years ago
Legacy ID: #3847370 Legacy Poster: Michael Kay (mhkay)
You seem to have missed out the step filter.setContentHandler(templatesHandler);
RE: XIncludeFilter Help [Fixed] - Added by Anonymous over 18 years ago
Legacy ID: #3847448 Legacy Poster: Kranklin (kranklin)
Almost got it. I left out what you said and put some paramaters where they dont belong. The code works without errors and processes the XSLT and XML, but does not filter the XInclude... which can be seen: http://new.a2zinventory.com/test.cfm . Unless there are any more suggestions, i'll try writing the xinclude support into my XSL. Anyway, Thank you Michael for spending so much time on my problem and of course for Saxon. For the record, here is my (possibly) final script: -------------------------------------------- // XSLInput, XMLInput, XMLSource variables not included // USED TO STORE RESULT xmlWriter = createObject("java", "java.io.StringWriter"); xmlWriter = xmlWriter.init(); xmlResult = createObject("java", "javax.xml.transform.stream.StreamResult"); xmlResult = xmlResult.init(xmlWriter); // Transform tfactory = createObject("java", "net.sf.saxon.TransformerFactoryImpl"); // Saxon // Create a Templates Content Handler to handle parsing of the stylesheet templatesHandler = tfactory.newTemplatesHandler(); //Create an XMLReader and set its features. spfactory = createObject("java", "javax.xml.parsers.SAXParserFactory"); spfactory = spfactory.newInstance(); reader = spfactory.newSAXParser(); reader = reader.getXMLReader(); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); //Create an XMLFilter that modifies the stylehseet filter = createObject("java", "com.elharo.xml.xinclude.XIncludeFilter"); filter.init(); filter.setParent(reader); filter.setContentHandler(templatesHandler); filter.parse(xslInput); // Get the Templates object (generated during the parsing of the stylesheet) from the TemplatesHandler. templates = templatesHandler.getTemplates(); // do the transformation //transformer = tFactory.newTransformer(xslSource); //--------------------------------------- This One works but does not perform the filter. transformer = templates.newTransformer(); //------------------------------------------- Error occurs Here: the functions seems to be returning null. transformer.transform(xmlSource, xmlResult); // OUTPUT THIS: xmlWriter.toString();
Please register to reply