Innes,
Here is a sample EMML snippet that converts string to doc.
<output name="result" type="document"/>
<variables>
<variable name="strData" type="string"/>
<variable name="tmpDoc" type="document" />
</variables>
<script type="javascript" outputvariable="strData">
<![CDATA[
strData = "<test><item>foo</item><item>bar</item></test>"
]]>
</script>
<assign fromvariable="$strData" outputvariable="$tmpDoc"/>
<constructor outputvariable="result">
<root>
{$tmpDoc//item}
</root>
</constructor>
Also, only well-formed XML document type or string type can be returned from EMML i.e., cannot return node-sets (that would typically be generated from assign statement referring to a node collection).
raj. chief masher @ jackbe
Hi Raj,
thanks for the reply. Here's the whole mashup code as i think that might help you. Even if I set a literal string inside the script, I still end up with the same encoded output.
<output name="result" type="document"/>
<variables>
<variable name="entry" type="document"></variable>
<variable name="id" type="string"></variable>
<variable name="streetname" type="string"></variable>
<variable name="city" type="string"></variable>
<variable name="xmlString" type="string" />
<variable name="xmlDoc" type="document"></variable>
<variable name="out" type="string" />
</variables>
<script type="groovy">
<![CDATA[
import groovy.xml.MarkupBuilder
import groovy.sql.Sql
def writer = new StringWriter()
def sql = groovy.sql.Sql.newInstance("jdbc:odbc:HY_ACCESS","sun.jdbc.odbc.JdbcOdbcDriver")
def bldr = new groovy.xml.MarkupBuilder(writer)
bldr.addressBook{
sql.eachRow("SELECT * FROM HY_ADDRESS"){ row ->
entry{
id(row.id)
streetname(row.streetname)
city(row.city)
}
}
}
xmlString = writer.toString()
println xmlString.getClass()
]]>
</script>
<assign outputvariable="$xmlDoc" fromvariable="$xmlString" />
<display variable="$xmlDoc" />
<constructor outputvariable="$result">
<root>{$xmlDoc}</root>
</constructor>
Hi Innes,
This appears to be a defect, will investigate and get back. However, have a workaround for this.
Just after your groovy scripting, introduce a temporary string variable (tmpStr), and use that as intermediary data conversion variable like in snippet below.
<!-- declare temp variable-->
<variable name="tmpStr" type="string"/>
<!-- assign groovy result xmlString to this tmpStr variable -->
<assign fromvariable="$xmlString" outputvariable="$tmpStr"/>
<!-- assign tmpStr variable to document type xmlDoc-->
<assign outputvariable="$xmlDoc" fromvariable="$tmpStr" />
<display variable="$xmlDoc" />
Here's a full script here for further illustration.
<mashup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jackbe.com/2008-03-01/EMMLSchema/ ../src/schemas/EMMLSpec.xsd"
xmlns="http://www.jackbe.com/2008-03-01/EMMLSchema"
name="GroovySample">
<output name="result" type="document"/>
<variables>
<variable name="entry" type="document"></variable>
<variable name="id" type="string"></variable>
<variable name="streetname" type="string"></variable>
<variable name="city" type="string"></variable>
<variable name="xmlString" type="string" />
<variable name="xmlDoc" type="document"></variable>
<variable name="out" type="string" />
</variables>
<script type="groovy" outputvariable="xmlString">
<![CDATA[
import groovy.xml.MarkupBuilder
import groovy.sql.Sql
def writer = new StringWriter()
writer.write(" <addressBook>");
writer.write(" <entry>");
writer.write(" <id>1</id>");
writer.write(" <streetname>5 Baker Road</streetname>");
writer.write(" <city>Bellevue</city>");
writer.write(" </entry>");
writer.write(" <entry>");
writer.write(" <id>2</id>");
writer.write(" <streetname>25 Bay St.</streetname>");
writer.write(" <city>Hull</city>");
writer.write(" </entry>");
writer.write(" <entry>");
writer.write(" <id>3</id>");
writer.write(" <streetname>251 Main St.</streetname>");
writer.write(" <city>W. York</city>");
writer.write(" </entry>");
writer.write("</addressBook>");
xmlString = writer.toString()
println '---------------------------------------'
println xmlString.getClass()
println '-----------------------------------------'
]]>
</script>
<variable name="tmpStr" type="string"/>
<assign fromvariable="$xmlString" outputvariable="$tmpStr"/>
<assign outputvariable="$xmlDoc" fromvariable="$tmpStr" />
<display variable="$xmlDoc" />
<constructor outputvariable="$result">
<root>{$xmlDoc//upper-case(city)}</root>
</constructor>
</mashup>
Let us know if this addresses your immediate problem.
raj. chief masher @ jackbe
Hi Raj,
thanks for the reply and the suggested workaround. After your inital post I set to work to see if I could figure out the problem myself and came up with a working solution late on Friday afternoon. I'm not sure why this works but you might be able to shed some light on the solution and what it was in the first place that was the problem.
I thought that perhaps it was an encoding problem of some kind since the string containing the xml was translated as it came out of the <constructor> tag. On investigation I found that java strings are UTF-16 encoded by default, whereas the XML output we are supposed to be generating is UTF-8 encoded. So I looked for a way to output a UTF-8 encoded string from my Groovy script. Once I did this and passed the output through an intermediate Javascript variable, it came out as a properly formed XML.
Here's the emml file with my working solution. I've highlighted the lines that I added to get the mashup output I originally expected.
<output name="result" type="document"/>
<variables>
<variable name="entry" type="document"></variable>
<variable name="id" type="string"></variable>
<variable name="streetname" type="string"></variable>
<variable name="city" type="string"></variable>
<variable name="xmlGString" type="string" />
<variable name="xmlJString" type="string" />
<variable name="xmlDoc" type="document"></variable>
<variable name="out" type="string" /> <!-- Needed for println output from Groovy script -->
</variables>
<script type="groovy" outputvariable="xmlGString" >
<![CDATA[
import groovy.xml.MarkupBuilder
import groovy.sql.Sql
def writer = new StringWriter()
def sql = groovy.sql.Sql.newInstance("jdbc:odbc:HY_ACCESS","sun.jdbc.odbc.JdbcOdbcDriver")
def bldr = new groovy.xml.MarkupBuilder(writer)
bldr.addressBook{
sql.eachRow("SELECT * FROM HY_ADDRESS"){ row ->
entry{
id(row.id)
streetname(row.streetname)
city(row.city)
}
}
}
String xmlStringHolder = writer
byte[] xmlBytes = xmlStringHolder.getBytes("UTF8")
xmlGString = new String(xmlBytes,"UTF8")
]]>
</script>
<script type="text/javascript" outputvariable="$xmlJString" inputvariables="xmlGString" >
<![CDATA[
var xmlJString = xmlGString
]]>
</script>
<assign fromvariable="$xmlJString" outputvariable="$xmlDoc"/>
<constructor outputvariable="$result" >
<root>{$xmlDoc}</root>
</constructor>
I may be completely off on the reason for the problem, but my workaround did the trick. If there is something wrong in the way I constructed the emml or the Groovy script, please let me know.
Hope this helps your investigation.
Cheers, Innes (NZ)



Hi mashers,
I'm having problems converting a string containing xml elements into a document type that I can then work on further within the mashup.
The string appears as follows:
<addressBook>
<entry>
<id>1</id>
<streetname>5 Baker Road</streetname>
<city>Bellevue</city>
</entry>
<entry>
<id>2</id>
<streetname>25 Bay St.</streetname>
<city>Hull</city>
</entry>
<entry>
<id>3</id>
<streetname>251 Main St.</streetname>
<city>W. York</city>
</entry>
</addressBook>
but when I attempt to convert it to a document type I get the following output:
<?xml version="1.0" encoding="UTF-8"?>
<root><addressBook>
<entry>
<id>1</id>
<streetname>5 Baker Road</streetname>
<city>Bellevue</city>
</entry>
....
</addressBook></root>
I used the following code to try to reform the string to a document type:
<assign fromvariable="$xmlString" outputvariable="$xmlDoc"/>
<constructor outputvariable="$result">
<root>
{$xmlDoc}
</root>
</constructor>
<assign fromexpr="$result/child::*" outputvariable="result" />
This is along the lines of a post to the forums explaining how to do a string-document XML conversion. Is there something I've missed or a better way to do this?
Thanks and cheers, Innes (NZ)