Set a List by setParameter (Transformator)
Added by Anonymous almost 20 years ago
Legacy ID: #2957596 Legacy Poster: A. P. (apfeuti)
Hi I have an XML document which I would like to transform to HTML with XSLT. I use Saxon 8.2. I don't want transform all records in the XML, only records which match some criterias. The criterias are dynamic and depends on user-input. My idea is: I parse the hole XML document and build a document (with the saxon-tree-model). Then I compute the XPath-Query an run it with Saxon-XPath. As a result I get a List with "Saxon-Nodes". This list I set as a transform-parameter. Then the XSLT-Script only operates on this nodes. How I understand documentation and several postings, this should work with Saxon, because Saxon allows to set a List as a parameter if the content is a "Saxon-tree-moel". But I allways get the following error. (At the end of this message, I post my Java-Code, the xsl-script and the xml-data. Note: For simplicity here the XPath-Query is hardcoded). Thank you very much for any help or hints. Andreas Error on line 11 of file:///N:/TwinChannelTesting/xslt/Test.xsl: XP0020: The context item for axis step attribute::attribute(Name) is not a node ; SystemID: file:///N:/TwinChannelTesting/xslt/Test.xsl; Line#: 11; Column#: -1 net.sf.saxon.xpath.DynamicError: The context item for axis step attribute::attribute(Name) is not a node at net.sf.saxon.expr.AxisExpression.iterate(AxisExpression.java:339) at net.sf.saxon.instruct.SimpleContentConstructor.evaluateItem(SimpleContentConstructor.java:151) at net.sf.saxon.instruct.SimpleNodeConstructor.expandChildren(SimpleNodeConstructor.java:91) at net.sf.saxon.instruct.ValueOf.processLeavingTail(ValueOf.java:146) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.instruct.ForEach.processLeavingTail(ForEach.java:220) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.expr.AppendExpression.process(AppendExpression.java:159) at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:217) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.expr.AppendExpression.process(AppendExpression.java:159) at net.sf.saxon.instruct.ElementCreator.processLeavingTail(ElementCreator.java:217) at net.sf.saxon.instruct.Instruction.process(Instruction.java:90) at net.sf.saxon.expr.AppendExpression.process(AppendExpression.java:159) at net.sf.saxon.instruct.Template.expand(Template.java:100) at net.sf.saxon.instruct.Template.processLeavingTail(Template.java:82) at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:285) at net.sf.saxon.Controller.transformDocument(Controller.java:1120) at net.sf.saxon.Controller.transform(Controller.java:964) Java-Code: //build the document with the JAXP-API. Use Saxon as implementation System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "net.sf.saxon.om.DocumentBuilderFactoryImpl"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource data = new InputSource(new FileInputStream(m_dataFile)); Node doc = builder.parse(data); System.setProperty("javax.xml.xpath.XPathFactory", "net.sf.saxon.xpath.XPathFactoryImpl"); //next line unfortunately does not work. JAXP can not find the Saxon-Factory, event the // property is //set correctly //XPathFactory xpathFactory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); XPathFactory xpathFactory = new XPathFactoryImpl(); //instantiate the Saxon-Factory by myself //Do the XPath-Query with the JAXP-API. Use Saxon as implementation XPath xpath = xpathFactory.newXPath(); List nodes = (List) xpath.evaluate("MyRoot/MyElement[@Name='Andreas']", doc, XPathConstants.NODESET); //Do the XSLT-Transformation with JAXP-API. Use Saxon as implementation System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); TransformerFactory transFormFactory = TransformerFactory.newInstance(); Source xslSource = new StreamSource(m_xslFehlerTicketsFile); // source needed by Transform-API, unfortunately its not the same type as needed by the // Document-Builder-API Source xmlSourceTransform = new StreamSource(m_dataFile); Transformer transformer = transFormFactory.newTransformer(xslSource); transformer.setParameter("myPara", nodes); transformer.transform(xmlSourceTransform, new StreamResult(out)); XSL: <?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:template match="/"> <xsl:param name="myPara"/> <html> <head> <title>Test-XSL</title> </head> <body bgcolor="#CCCCCC"> <xsl:for-each select="$myPara"> <xsl:value-of select="@Name"/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> XML: <?xml version="1.0" encoding="UTF-8"?> <MyRoot> <MyElement Id="1" Name="Andreas" MyValue="Test1"/> <MyElement Id="2" Name="Andreas" MyValue="Test2"/> <MyElement Id="3" Name="Mike" MyValue="Test3"/> <MyElement Id="4" Name="Andreas" MyValue="Test4"/> <MyElement Id="5" Name="Juan" MyValue="Test5"/> </MyRoot> Note: In the Java-Code I had the following problem: JAXP could not find the Saxon-XPathFactory, even I set the system-property. As a workaraound I instantiaded the XPathFactory by myself. Could my transform-problem depend on this "minor"-problem? I don't think so.
Replies (2)
RE: Set a List by setParameter (Transformator) - Added by Anonymous almost 20 years ago
Legacy ID: #2957717 Legacy Poster: Antony Blakey (ablakey)
A cursory glance suggest that you should at least move the param outside of the template i.e.: <?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="myPara"/> <xsl:template match="/"> <html> <head> <title>Test-XSL</title> </head> <body bgcolor="#CCCCCC"> <xsl:for-each select="$myPara"> <xsl:value-of select="@Name"/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
RE: Set a List by setParameter (Transformator) - Added by Anonymous almost 20 years ago
Legacy ID: #2957770 Legacy Poster: A. P. (apfeuti)
That's it! I was looking for the error at the wrong place... Thank you very much! Andreas
Please register to reply