使用WSO2 ESB进行XSLT转换的问题

时间:2014-10-08 08:51:08

标签: java xml xslt wso2

我在wso2 esb 4.8.1中使用XSLT介体与XSLT转换器介体进行了斗争。 xslt是:

<xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
        xmlns:ns="http://ep.service.ims.com"
        xmlns:ax21="http://ep.service.ims.com/xsd"
        exclude-result-prefixes="ns fn">
    <xsl:param name="amount"/>
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//ns:getResponse" />
    </xsl:template>

    <xsl:template match="ns:getResponse" xmlns:ns="http://ep.service.ims.com">
        <ep:credit xmlns:ep="http://ep.service.ims.com" xmlns:xsd="http://ep.service.ims.com/xsd">
            <ep:info>
                <xsd:amount>
                    <xsl:value-of select="$amount"/>
                </xsd:amount>
                <xsd:personInfo>
                    <xsd:address>
                        <xsl:value-of select="ns:return/ax21:address"/>
                    </xsd:address>
                    <xsd:id>
                        <xsl:value-of select="ns:return/ax21:id"/>
                    </xsd:id>
                    <xsd:name>
                        <xsl:value-of select="ns:return/ax21:name"/>
                    </xsd:name>
                </xsd:personInfo>
            </ep:info>
        </ep:credit>
    </xsl:template>
</xsl:stylesheet>

并且请求XML是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:getResponse xmlns:ns="http://ep.service.ims.com">
         <ns:return xsi:type="ax23:PersonInfo" xmlns:ax23="http://ep.service.ims.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ax23:address>IMS Heath, Omega C, India, bnag</ax23:address>
            <ax23:id>100</ax23:id>
            <ax23:name>WSO2</ax23:name>
         </ns:return>
      </ns:getResponse>
   </soapenv:Body>
</soapenv:Envelope>

我尝试了eclipse中的XSLT转换和一些像(http://xslt.online-toolz.com/tools/xslt-transformation.php)这样的在线工具,它运行正常。然而,当我在WSO2 ESB中尝试相同时,我面临以下异常......

org.apache.synapse.mediators.transform.XSLTMediator} -  
   Fatal error occurred in stylesheet parsing : 
   net.sf.saxon.trans.XPathException: 
   The supplied file does not appear to be a stylesheet Value {name ='null', keyValue ='xslt1'} 
{org.apache.synapse.mediators.transform.XSLTMediator}
   javax.xml.transform.TransformerConfigurationException: Failed to compile stylesheet. 1 error detected.
   at net.sf.saxon.PreparedStylesheet.prepare(PreparedStylesheet.java:220)
org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:170)
   at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
   at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:225)
TID: [0] [ESB] [2014-10-08 13:53:20,705] ERROR   
{org.apache.synapse.mediators.transform.XSLTMediator} -  
   Unable to perform XSLT transformation using : 
   Value {name ='null', keyValue ='xslt1'} against source XPath : s11:Body/child::*[position()=1] | s12:Body/child::*[position()=1] 
   reason : Error creating XSLT transformer using : Value {name ='null', keyValue ='xslt1'} 
{org.apache.synapse.mediators.transform.XSLTMediator}
   org.apache.synapse.SynapseException: Error creating XSLT transformer using : Value {name ='null', keyValue ='xslt1'} at  
org.apache.synapse.mediators.AbstractMediator.handleException(AbstractMediator.java:313)
   Caused by: javax.xml.transform.TransformerConfigurationException: 
   Failed to compile stylesheet. 1 error detected.

synapse proxy xml是:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="CreditProxy" transports="https http" startOnLoad="true" trace="disable">
    <target>
      <inSequence>
         <log level="full">
            <property name="sequence" value="inSequence - request for CreditProxy"/>
         </log>
         <property xmlns:pep="http://com.ims.proxy" name="ORG_ID" expression="//pep:credit/pep:id"/>
         <property xmlns:pep="http://com.ims.proxy" name="ORG_AMOUNT" expression="//pep:credit/pep:amount"/>
         <enrich>
            <source type="inline" clone="true">
                <pep:get xmlns:pep="http://ep.service.ims.com">
                    <pep:id>?</pep:id>
                </pep:get>
            </source>
            <target type="body"/>
         </enrich>
         <enrich>
            <source type="property" property="ORG_ID"/>
            <target xmlns:pep="http://ep.service.ims.com" xpath="//pep:get/pep:id"/>
         </enrich>
         <log level="full">
            <property name="sequence" value="inSequence - request for PersonInfoService"/>
         </log>
         <property name="STATE" value="PERSON_INFO_REQUEST"/>
         <send>
            <endpoint key="PersonInfoEpr"/>
        </send>
     </inSequence>
     <outSequence>
        <switch source="get-property('STATE')">
            <case regex="PERSON_INFO_REQUEST">
                <log level="full">
                    <property name="sequence" value="outSequence - STATE 01 - response from PersonInfoService"/>
                </log>
                <xslt key="xslt">
                    <property name="amount" expression="get-property('ORG_AMOUNT')"/>
                </xslt>
                <log level="full">
                    <property name="sequence" value="outSequence - STATE 01 - request for CreditService"/>
                </log>
                <property name="STATE" value="CREDIT_REQUEST"/>
                <send>
                    <endpoint key="CreditEpr"/>
                </send>
            </case>
            <case regex="CREDIT_REQUEST">
                <log level="full">
                    <property name="sequence" value="outSequence - STATE 02 - response from CreditService"/>
                </log>
                <send/>
            </case>
        </switch>
    </outSequence>
  </target>
  <publishWSDL uri="file:resources/CreditProxy.wsdl"/>
</proxy>

如果XSLT转换在其他工具中正常工作,可能是异常的原因是什么?

2 个答案:

答案 0 :(得分:0)

代理中的密钥是正确的。我可能粘贴了错误的XML代理,

这是代理:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="CreditProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="full">
            <property name="sequence" value="inSequence - request for CreditProxy"/>
         </log>
         <property xmlns:pep="http://com.ims.proxy"
                   name="ORG_ID"
                   expression="//pep:credit/pep:id"/>
         <property xmlns:pep="http://com.ims.proxy"
                   name="ORG_AMOUNT"
                   expression="//pep:credit/pep:amount"/>
         <enrich>
            <source type="inline" clone="true">
               <pep:get xmlns:pep="http://ep.service.ims.com">
                  <pep:id>?</pep:id>
               </pep:get>
            </source>
            <target type="body"/>
         </enrich>
         <enrich>
            <source type="property" clone="true" property="ORG_ID"/>
            <target xmlns:pep="http://ep.service.ims.com" xpath="//pep:get/pep:id"/>
         </enrich>
         <log level="full">
            <property name="sequence" value="inSequence - request for PersonInfoService"/>
         </log>
         <property name="STATE" value="PERSON_INFO_REQUEST"/>
         <send>
            <endpoint key="PersonInfoEpr"/>
         </send>
      </inSequence>
      <outSequence>
         <switch source="get-property('STATE')">
            <case regex="PERSON_INFO_REQUEST">
               <log level="full">
                  <property name="sequence"
                            value="outSequence - STATE 01 - response from PersonInfoService"/>
               </log>
               <xslt key="xslt1">
                  <property name="amount" expression="get-property('ORG_AMOUNT')"/>
               </xslt>
               <log level="full">
                  <property name="sequence"
                            value="outSequence - STATE 01 - request for CreditService"/>
               </log>
               <property name="STATE" value="CREDIT_REQUEST"/>
               <send>
                  <endpoint key="CreditEpr"/>
               </send>
            </case>
            <case regex="CREDIT_REQUEST">
               <log level="full">
                  <property name="sequence"
                            value="outSequence - STATE 02 - response from CreditService"/>
               </log>
               <send/>
            </case>
         </switch>
      </outSequence>
   </target>
   <publishWSDL uri="file:resources/creditproxy/CreditProxy.wsdl"/>
   <description/>
</proxy>

本地参赛作品:

<localEntry xmlns="http://ws.apache.org/ns/synapse" key="xslt1" src="file:resources/creditproxy/personToCredit.xslt"></localEntry>

答案 1 :(得分:0)

本地条目存在问题。最后,我能够通过在synapse config

中添加这样的本地条目来解决这个问题
   <localEntry key="xslt99">
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      xmlns:ax21="http://ep.service.ims.com/xsd"
                      xmlns:ns="http://ep.service.ims.com"
                      xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
                      version="1.0"
                      exclude-result-prefixes="ns fn">
         <xsl:param name="amount"/>
         <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
         <xsl:template match="/">
            <xsl:apply-templates select="//ns:getResponse"/>
         </xsl:template>
         <xsl:template match="ns:getResponse">
            <ep:credit xmlns:ep="http://ep.service.ims.com" xmlns:xsd="http://ep.service.ims.com/xsd">
               <ep:info>
                  <xsd:amount>
                     <xsl:value-of select="$amount"/>
                  </xsd:amount>
                  <xsd:personInfo>
                     <xsd:address>
                        <xsl:value-of select="ns:return/ax21:address"/>
                     </xsd:address>
                     <xsd:id>
                        <xsl:value-of select="ns:return/ax21:id"/>
                     </xsd:id>
                     <xsd:name>
                        <xsl:value-of select="ns:return/ax21:name"/>
                     </xsd:name>
                  </xsd:personInfo>
               </ep:info>
            </ep:credit>
         </xsl:template>
      </xsl:stylesheet>
      <description/>
   </localEntry>

最初我试图添加像这样的本地条目

<localEntry xmlns="http://ws.apache.org/ns/synapse" key="xslt1" src="file:resources/creditproxy/personToCredit.xslt"></localEntry>

但我仍然不知道如何在本地条目中指定xslt文件。?