Jython : Issues executing XSLT

家住魔仙堡 提交于 2019-12-11 15:03:40

问题


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

来源:https://stackoverflow.com/questions/46572227/jython-issues-executing-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!