How do I config xml namespace for a smooks input file using WSO2? - eclipse

I couldn't find any advices on how to setup Smooks (on WSO2 Developer Studio 3.1) in order to it properly read a xml input which has a namespace declaration.
Without the namespace the transformation works just fine!
By informing the xmlns in the input, I get this exception:
Error on line 5, column 19 in free-marker-template
Expecting a string, date or number here, Expression .vars["order"]["order-items/order-item/#id"] is instead a freemarker.ext.dom.NodeListModel
The problematic instruction:
----------
==> ${.vars["order"]["order-items/order-item/#id"]} [on line 5, column 17 in free-marker-template]
These are both my input and output:
<order id='444' xmlns="http://example.com">
<header>
<customer number="555">Amila</customer>
</header>
<order-items>
<order-item id='1'>
<product>1</product>
<quantity>2</quantity>
<price>400</price>
</order-item>
</order-items>
</order>
<salesorder>
<details>
<orderid></orderid>
<customer>
<id></id>
<name></name>
</customer>
</details>
<itemList>
<item>
<id></id>
<productId></productId>
<quantity></quantity>
<price></price>
</item>
</itemList>
</salesorder>
I've also tried Smooks coreĀ“s namespace declarations as per Smooks site:
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd"
>
<core:namespaces>
<core:namespace prefix="ex" uri="http://example.com/"/>
</core:namespaces>
But it seems not to be supported in the IDE since the configuration editor raises this exception:
-Value 'org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl#53abd5a9 (mixed: null, anyAttribute: null)' is not legal. (platform:/resource/Corp/smooks-config.xml,6,20)
Well, any idea?

Instead of Smooks, can't you use a XSLT? It will avoid the namespace issue you have.

Related

XSLT streaming not streaming

I'm using Saxon-EE for the purpose of streaming XSLT transformation of large XML. The transformation works fine but it seems it's not really streaming since the java.exe process is inflating: for a 100 MB XML, process memory increases ~1GB. This is the XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:bb="urn:xx-zz-1.1"
xmlns:aa="urn:xx-yy-1.1">
<xsl:mode streamable="yes"/>
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:for-each select="aa:LevelOne/aa:LevelTwo">
<xsl:iterate select="bb:LevelThree! copy-of(.)">
<xsl:value-of select="concat(bb:fieldOne,',',bb:fieldTwo,'
')"/>
</xsl:iterate>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This is the XML:
<?xml version="1.0" encoding="utf-8"?>
<aa:LevelOne xmlns="urn:xx-zz-1.1" xmlns:aa="urn:xx-yy-1.1">
<aa:LevelTwo xmlns="urn:xx-zz-1.1" xmlns:aa="urn:xx-yy-1.1">
<LevelThree xmlns="urn:xx-zz-1.1">
<fieldOne>f1</fieldOne>
<fieldTwo>f2</fieldTwo>
</LevelThree>
<!-- Level three is repeated many times -->
</aa:LevelTwo>
</aa:LevelOne>
I would like to know if there (& what) is a problem with the XSLT above.
The code I use:
net.sf.saxon.s9api.Processor processor = new net.sf.saxon.s9api.Processor(true);
processor.setConfigurationProperty(Feature.STREAMABILITY, "standard");
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable stylesheet = compiler.compile(new StreamSource(stylesheetFile));
Serializer out = processor.newSerializer(outputCsvFile);
Xslt30Transformer transformer = stylesheet.load30();
transformer.applyTemplates(new StreamSource(xmlFile), out);
EDIT: Fixed the XSLT so it compiles & added XML example.
Remark: using command java -cp "<path>\test;<path>\saxon9ee.jar" com.example.test.Test -t does not ouput additional info (only the printlns in the code). java -cp "<path>\test;<path>\saxon9ee.jar" -t com.example.test.Test outputs: Unrecognized option: -Xt Error: Could not create the Java Virtual Machine. If I change the XSLT to non-streamable rule e.g. remove the iterate line, program outputs Template rule is not streamable, also without -t option. In this case if I remove the streamability requirement from code/xslt, the error goes away.
Thanks.
Probably the most likely reason Saxon would fall back to non-streaming mode is that it hasn't located a Saxon-EE license. The easiest way to test that is (unintuitively!) by calling processor.isSchemaAware() - that will only be true if you're running Saxon-EE code with a recognized license, which is exactly the same condition to enable streaming.
If it hasn't found a license, the Saxon documentation includes a section on troubleshooting license problems at http://www.saxonica.com/documentation/index.html#!about/license
Also, try it from the command line with option -t; that will give you more information (a) about streaming, and (b) about loading of license files.
I think, if the data is as simple and as regular as shown in the question, then you can avoid the use of copy-of() and simply use
<xsl:iterate select="bb:LevelThree">
<xsl:value-of select="bb:*" separator=","/>
<xsl:text>
</xsl:text>
</xsl:iterate>
That in a quick test shows a reduced memory consumption compared to your posted approach.
As for your posted approach not using streaming with Saxon EE 9.9, I have tested the posted XSLT and the input sample with Saxon 9.9 EE from the command line with the -t option and it shows the input is streamed.
I also think the Java code shown is fine to process the file with streaming with Saxon EE.
For a detailed analysis of the memory consumption and any problems you encounter with that it might be better to raise an issue with all details on the Saxonica support site. I am not sure how the memory info Saxon outputs relates exactly to the one you say you see for java.exe.

Optaplanner 7.11: id/reference error posting xml for solution to kie-server

I'm tryng samples projects to understand how to use the Optaplanner features (using ver. 7.11-Final on wildfly11). I installed some built-in samples and now I encountered a very strange error after using an
employee rostering sample modified by me, when I try to post different xml via REST API (using SOAP-UI).
Using the following xml (excerpt) it work well, I can submit the xml without problem and retrieve the best solution calling the related endpoint:
<TeamRoster>
<employeeList>
<Employee>
<name>Luigi</name>
</Employee>
<Employee>
<name>Mauro</name>
</Employee>
<Employee>
<name>Pietro</name>
</Employee>
<Employee>
<name>Gino</name>
</Employee>
</employeeList>
<teamList>
<Team>
<name>Squadra 1</name>
<employeeList>
<Employee reference="../../../../employeeList/Employee"/>
<Employee reference="../../../../employeeList/Employee[2]"/>
</employeeList>
.........
Instead, using the following modified xml (with id/reference):
<TeamRoster id="1">
<employeeList id="2">
<Employee id="3">
<id>0</id>
<name>Luigi</name>
</Employee>
<Employee id="4">
<id>1</id>
<name>Mauro</name>
</Employee>
<Employee id="5">
<id>2</id>
<name>Pietro</name>
</Employee>
<Employee id="6">
<id>3</id>
<name>Gino</name>
</Employee>
</employeeList>
<teamList id="7">
<Team id="8">
<id>0</id>
<name>Squadra 1</name>
<employeeList id="9">
<Employee reference="3"/>
<Employee reference="4"/>
</employeeList>
The response of kie server is a message error:
com.thoughtworks.xstream.converters.ConversionException: Invalid reference
with details:
message : Invalid reference
reference : 3
referenced-type : com.linkit.trostering.Employee
referenceable : true
class : java.util.ArrayList
required-type : java.util.ArrayList
converter-type : com.thoughtworks.xstream.converters.collections.CollectionConverter
path : /TeamRoster/teamList/Team/employeeList/Employee
line number : 25
class[1] : com.linkit.trostering.Team
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[2] : com.linkit.trostering.TeamRoster
version : 7.11.0.Final
I've seen that other samples included in optaplanner samples project use this option (if I understand it's a xtream feature) to reference fact already defined using id instead the xml path.
Now my doubt is if Optaplanner wb with kie server is shipped with xstream different settings? Is there any option that must be send via REST to use this feature of xtream? Any other thing?
Thanks in advance for any help that is very appreciated
Your first xml uses XPath references. Your second xml uses id references.
The second one requires a different XStream setup:
xStream = new XStream();
xStream.setMode(XStream.ID_REFERENCES);

Xpath compilation for Child element with namespace + Getting error - Prefix must resolve to a namespace: ns2

Sample
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<ns2:bookStore xmlns:ns2="http://bookstore.com/schemes">
<ns2:book id="1">
<ns2:name>Data Structure</ns2:name>
</ns2:book>
<ns2:book id="2">
<ns2:name>Java Core</ns2:name>
</ns2:book>
</ns2:bookStore>
</Root>
Java code
XPathExpression expr =
xpath.compile("//Root/ns2:bookStore/ns2:book/ns2:name/text()");
I get Prefix must resolve to a namespace: ns2 error with above xml with Root
tag. By removing the Root tag I'm able to parse the data. Any help would be appreciated without modifying the XML file.

MyBatis: Error when adding a 'bind' inside of 'foreach'

I'm using MyBatis for handling SQL queries. Here is my problematic piece of code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.foo.Foo">
...
<insert id="insert" parameterType="com.foo.FooParam">
<foreach collection="bars" item="bar" separator=" ">
<bind name="inFavorites" value="bar.sectionId == '_favorites'" />
<foreach collection="bar.bars2" item="bar2" index="index" separator=" ">
...
</foreach>
</foreach>
</insert>
...
</mapper>
Intelij shows the following error when validiting the xml:
Error:(24, 74) Element type "bind" must be declared.
Error:(28, 19) The content of element type "foreach" must match "(include|trim|where|set|foreach|choose|if)".
And when i put a compiled module as an osgi package in a jetty server (mvn compiles it w/o errors and warnings) I'm getting the following error:
lineNumber: 28; columnNumber: 19; The content of element type "foreach"
must match "(include|trim|where|set|foreach|choose|if)".
So Ok, I get this. I cannot add a 'bind' element inside of a 'foreach'.
But why, if http://mybatis.org/dtd/mybatis-3-mapper.dtd says otherwise?
<!ELEMENT foreach (#PCDATA | include | trim | where | set | foreach | choose | if | bind)*>
bind inside foreach is allowed since version 3.2.3. You are obviously using an older version.
dtd file is not downloaded from the internet but the version packaged in mybatis jar is used.

Calabash 1.0.23 throws on xsl-formatter step: ERROR: Failed to instantiate FO provider

I'm trying to use XML Calabash 1.0.23 to run XSLT transformation and FO formatting in a single pipeline. Although the XSLT step works fine, I cannot get the xsl-formatter step to work with FOP.
Every time I run the pipeline, Calabash throws:
ERROR: pipeline.xpl:13:68:Failed to instantiate FO provider
ERROR: Underlying exception: org/apache/fop/apps/FopFactory
My call to Calabash from the command line is:
java com.xmlcalabash.drivers.Main -c cfg.xml myPipeline.xpl
And the cfg.xml configuration file being referred to in the above line is:
<cc:xproc-config xmlns:cc="http://xmlcalabash.com/ns/configuration">
<cc:fo-processor class-name="com.xmlcalabash.util.FoFOP"/>
</cc:xproc-config>
For some reason, Calabash seems to ignore the config file setting, because regardless of the value of the class-name attribute on <cc:fo-processor>, it always throws the same error message. For example, if I use com.xmlcalabash.util.FoAH, the same happens; and if put a nonsensical value, the same occurs. It's always an exception on org/apache/fop/apps/FopFactory.
Just for the sake of completeness, this is my XPL:
<declare-step name="main" version="1.0" xmlns="http://www.w3.org/ns/xproc">
<input port="parameters" kind="parameter" />
<xslt name="transformation">
<input port="source">
<document href="myMarkup.xml" />
</input>
<input port="stylesheet">
<document href="myStylesheet.xsl" />
</input>
</xslt>
<xsl-formatter href="newDoc.pdf" >
<input port="source">
<pipe step="transformation" port="result" />
</input>
</xsl-formatter>
</declare-step>
Of course, if I manually pass the generated FO from the XSLT step to FOP 1.1, it converts it to PDF with no problems. The issue only arises when trying to do the conversion within the pipeline.
I could really use some help to solve this. I'm clueless at this point.
This may seem like a very pedantic reply, but do you have fop.jar (and fop-hyph.jar, which I think FOP requires) on your classpath? They're not bundled in the XML Calabash distribution, you have to get them from Apache.