Forum Discussion

tomaszarmata's avatar
tomaszarmata
Contributor
9 years ago
Solved

Differences in output after execute the same groovy script inside and outside soapUI.

Hello,

 

I want to transform XML into HTML using groovy and XSL file. I use javax.xml.transform library for this. But when I try run groovy script inside and outside soapUI it it gives me diffrent responses.

 

Here are my test files:

 

<?xml version="1.0" encoding="UTF-8"?>
<apis>
    <api min="" max="2.0">
        <resource name="A">
            <description>A from beggining to 2.0</description>
        </resource>
    </api>
    <api min="2.0" max="2.0.1">
        <resource name="A">
            <description>A from 2.0 to 2.0.1</description>
        </resource>
    </api>
    <api min="2.0.1" max="">
        <resource name="A">
            <description>A from 2.0.1 to now</description>
        </resource>
    </api>
</apis>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="api-version" select="2.0"/>

<xsl:template match="/apis">
    <html>
        <head/>
        <body>
            <table border="1">
                <th colspan="2">
                    <xsl:text>Doc for API: </xsl:text>
                    <xsl:value-of select="$api-version"/>
                </th>
                <xsl:apply-templates select="api[(@min='' or @min &lt;= $api-version) and (@max='' or @max &gt; $api-version)]/resource">
                    <xsl:sort select="@name"/>
                </xsl:apply-templates>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="resource">
    <tr>
        <td>
            <xsl:value-of select="@name"/>
        </td>
        <td>
            <xsl:value-of select="description"/>
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>
import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

def workspacePath
def xslPath
def xslFileName
def xmlPath
def xmlFileName
def outputPath
def outputFileName
def xslt
def transformer
def xml
def html
def apiVersions

//define workspace
workspacePath = "C:/test/"
//define xls location
xslPath = "transformations/"
//define xsl file name
xslFileName = "test4.xsl"
//define source xml location
xmlPath = "pendingFeature/"
//define xml file name
xmlFileName = "test4.xml"
//define output path
outputPath = "outputs/"
//define api version
apiVersions = ['1.0','2.0','2.0.1']

// load xslt
xslt = new File(workspacePath + xslPath + xslFileName).getText()
// create transformer
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt)))
// load xml
xml = new File(workspacePath + xmlPath + xmlFileName).getText()

apiVersions.each { apiVersion ->
	//define output file name
	outputFileName = "doc for " + apiVersion + " api version.html"
	// set output file
	html = new FileOutputStream(workspacePath + outputPath + outputFileName)
	// set api version
	transformer.setParameter("api-version", apiVersion)
	// perform transformation
	transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(html))
	//close stream
	html.close()
}

For API version 1.0 it is ok, but for 2.0 and higher there is a problem while running script outside soapUI with finding matching api node with expresion

(@min='' or @min &lt;= $api-version) and (@max='' or @max &gt; $api-version)

I think it is because in XPath 1.0 the < and > operators always do numeric comparison and 2.0 doesn't. But I declare in XSL files xsl:stylesheet version="2.0" so is should use XPath 2.0 in both places and work. W3schools says:

"XSLT 2.0, XPath 2.0, and XQuery 1.0, share the same functions library. There are over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more."

 

Why in soapUI it works?

  • Its because of diffrent XML processor used. SoapUI uses Saxon which supports XPath 2.0 and running script outside groovy use Xalan which doesn't support XPath 2.0.

1 Reply

  • Its because of diffrent XML processor used. SoapUI uses Saxon which supports XPath 2.0 and running script outside groovy use Xalan which doesn't support XPath 2.0.