Jython : Issues executing XSLT

2019-08-23 01:48发布

问题:

Ignoring identation, below Jython code when executed from command line produces, output with self-closing tags(No data), as shown in the picture below

from java.io import *
import javax.xml
from java.io import StringReader, StringWriter
from javax.xml.transform import Transformer, TransformerFactory
from javax.xml.transform.stream import StreamResult, StreamSource

def obtainservicexslts():

print "In obtainservicexslts"    
print
source_xform_xml = """<?xml version='1.0' encoding='UTF-8'?>
<service-config>
  <service path="/Market">
    <request>         
      <rule transform="local:///services/xslt/MarketRequest.xslt">Transform</rule>
    </request>
    <response>
      <rule transform="local:///services/xslt/MarketResponse.xslt">Transform</rule>          
    </response>        
  </service>
</service-config>"""

source_xform_xslt = """<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="xsltkey" match="/*/*/*/rule[contains(., 'Transform')]/@transform" use="."/>
<xsl:template match="/" mode="xsltname">
    <xsl:variable name="firstoutput">
        <xsl:call-template name="firstpass"/>
    </xsl:variable>
    <xsl:apply-templates select="mutate">            
        <xsl:with-param name="pr" select="exsl:node-set($firstoutput)/*"/>
    </xsl:apply-templates>
</xsl:template>
<xsl:template match="/*" name="firstpass">
    <dpXsltTransforms>
        <xsl:for-each select="/*/*/*/rule[contains(., 'Transform')]/@transform[generate-id()
                                   = generate-id(key('xsltkey',.)[1])]">
            <transform-dpPath>
                <xsl:value-of select="."/>
            </transform-dpPath>
        </xsl:for-each>
    </dpXsltTransforms>
</xsl:template>
<xsl:template match="mutate">
    <xsl:param name="pr"/>
    <services-xslt>            
        <xsl:for-each select="pr/transform-dpPath[contains(., 'local:///services/xslt/')]">
            <xslt-cognomen>
                <xsl:value-of select="substring(.,24)"/>
            </xslt-cognomen>
        </xsl:for-each>
    </services-xslt>
</xsl:template>
</xsl:stylesheet>"""

try:
    transformer = TransformerFactory.newInstance().newTransformer(StreamSource(StringReader(source_xform_xslt)))
    output_buffer = StringWriter()       
    transformer.transform(StreamSource(StringReader(source_xform_xml)), StreamResult(output_buffer))
    print output_buffer.buffer.toString()        
    return output_buffer.buffer.toString()   
except:                
    raise


if __name__ == '__main__':
    obtainservicexslts()

​If we take 'xml' and 'xslt' from the script ​, save as ​2 separate files and execute the transform in XMLSPY​, below output is observed

The output from XMLSPY is desired and I'm not able to replicate the same behavior using my code.

Another point to be noted, I have used 'exsl' as part of xslt. Not sure, if that matters

Did anybody encountered similar issue and how was it resolved?

Kindly throw some pointers

I'm open to adjust the code either in xslt or jython to get things done