忽略标识,从命令行执行时,在Jython代码下面生成,输出带有自关闭标记(无数据),如下图所示
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()
如果我们从脚本中取出'xml'和'xslt',保存为2个单独的文件并在XMLSPY中执行转换,则会在下面输出
需要XMLSPY的输出,我无法使用我的代码复制相同的行为。
另外需要注意的一点是,我使用'exsl'作为xslt的一部分。不确定,如果重要
是否有人遇到类似的问题以及如何解决?
请提出一些指示
我愿意在xslt或jython中调整代码以完成任务