如何在soapServer响应中返回自定义XML响应? [英] How return custom XML response in soapServer response?
问题描述
我正在设置一个SOAP Web服务,该服务接受XML输入并必须返回自定义XML输出. 所有这些都在WSDL中定义.我为此应用了soapServer(直到有人说它存在阻止我实现目标的错误:-)).
I am setting up a SOAP webservice which takes XML input and has to return custom XML output. All this is defined in a WSDL. I apply soapServer for this (until someone says it has bugs preventing me from achieving my goal :-)).
我还无法返回自定义XML:我得到的结果似乎是基于WSDL的,其标准根元素名称等于输入XML加"Response". 实际上,这也使我感到惊讶,因此作为一个附带问题,我想知道为什么会这样以及它是否会受到影响.当然,在创建响应时以某种方式使用WSDL定义是一件好事,但是正如我所说,我不知道如何在响应中获取定制XML.
I haven't been able yet to return custom XML: I get some result which seems to be based on teh WSDL, with a standard root element name equal to the input XML one plus "Response". Actually that surprises me too so as a side question I wonder why that is and whether it can be influenced. Of course it is a nice thing that the WSDL definitions are used somehow when responses are created, but as I said, I don't know how to get custom XML in the response.
我了解到了:
WSDL
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://pse/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
name="PSE"
targetNamespace="http://pse/">
<types>
<xs:schema>
<xs:import namespace="http://pse/" schemaLocation="PSE.xsd"/>
</xs:schema>
</types>
<message name="MI102Req">
<part name="cdhead" type="tns:cdhead_T"/>
<part name="instr" type="tns:instr_T"/>
</message>
<message name="Res">
<part name="cdhead" type="tns:cdhead_T"/>
</message>
<portType name="MIPortType">
<operation name="mi102">
<input message="tns:MI102Req"/>
<output message="tns:Res"/>
</operation>
</portType>
<binding name="MIBinding" type="tns:MIPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="mi102">
<soap:operation soapAction="http://www.testURL/test_soap.php#mi102"/>
<input>
<soap:body use="literal" namespace="http://pse/"/>
</input>
<output>
<soap:body use="literal" namespace="http://pse/"/>
</output>
</operation>
</binding>
<service name="PSE">
<port name="MIPortType" binding="tns:MIBinding">
<soap:address location="http://www.testURL/test_soap.php"/>
</port>
</service>
</definitions>
输入XML
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<mi102 xmlns="http://pse">
<cdhead version="13"/>
<instr/>
</mi102>
</Body>
</Envelope>
当前php
<?php
class PSE {
function mi102 ($stdClassInput) {
$inp = file_get_contents ('php://input');
$xml = simplexml_load_string ($inp); // Envelope
$ch = $xml -> children ();
$elt1 = $ch [0]; // Body
$ch = $elt1 -> children ();
$elt2 = $ch [0]; //mi102
$xslt = new XSLTProcessor();
$xslt -> registerPHPFunctions();
$xslt -> importStylesheet ( DOMDocument::load ('test.xslt') );
$dom = $xslt -> transformToDoc (DOMDocument::loadXML ($elt2 -> asXML()));
$result = new SoapVar ($dom -> saveXML(), XSD_ANYXML);
return ($result);
}
}
ini_set( "soap.wsdl_cache_enabled", "0");
$server = new SoapServer ("test.wsdl");
$server -> setClass ('PSE');
$server -> setObject (new PSE());
$server -> handle();
?>
上面使用的XSLT只是一种更改属性-而temp总是将根名称更改为服务器返回的根名称(以防万一:-))
XSLT used above is just one changing an attribute - and temp change the root name to the one returned by the server always (just in case :-))
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pse="http://pse">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="pse:mi102">
<mi102Response>
<xsl:apply-templates/>
</mi102Response>
</xsl:template>
<xsl:template match="pse:cdhead">
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
<xsl:template match="@version">
<xsl:attribute name="version">14</xsl:attribute>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
我希望返回的XML类似于
I expect the return XML to be something like
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://pse/">
<SOAP-ENV:Body>
<ns1:mi102Response>
<cdhead version="14"/>
</ns1:mi102Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
但实际上是
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://pse/">
<SOAP-ENV:Body>
<ns1:mi102Response/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
在上面的php中调试$ dom内容将准确显示我尝试返回的XML(当然,包装在SOAP Envelope/Body中,就像输入的内容一样):
Debugging $dom contents in above php shows exactly the XML I try to return (wrapped in SOAP Envelope/Body, of course, just like the input one):
<?xml version="1.0" encoding="UTF-8"?>
<mi102Response xmlns:pse="http://pse">
<cdhead xmlns="http://pse" version="14"/>
</mi102Response>
我哪里出错了? 如何将自定义XML放入返回的http响应内容中?
Where do I go wrong? How get custom XML into the returned http response content?
推荐答案
Ph!
这花了我几次重试和谷歌搜索,直到我发现出了什么问题.
我认为可以将其归类为SoapVar
中的错误.
This took me several retries and googling until I discovered what is wrong.
I think it can be classified as a bug in SoapVar
.
我发现,虽然SoapVar完全能够解析XML字符串,但是如果字符串包含诸如<?xml version="1.0" encoding="UTF-8"?>
之类的XML声明,它就无法解析.
因此,当您使用DOMDocument
或SimpleXMLElement
时,首先必须删除声明,然后再通过SoapVar解析字符串.
I discovered that while SoapVar is perfectly capable of parsing an XML string, it cannot do so if the string contains an XML declaration like <?xml version="1.0" encoding="UTF-8"?>
.
So when you have a DOMDocument
or a SimpleXMLElement
, you first have to strip off the declaration before parsing the string by SoapVar.
对于DOMDocument
,这可以通过使用参数saveXML
来实现,该参数等于从dom本身构造的DOMNode
变量,选择任何节点,但通常当然是根节点.
for a DOMDocument
this can be done by applying saveXML
with a parameter equal to a DOMNode
variable constructed from the dom itself, choosing any node but usually this will be the root node of course.
在服务器php中,添加了以下内容:
In my server php, I added the following:
$nodes = $dom -> getElementsByTagName ('cdhead');
$node = $nodes -> item(0);
$result = new SoapVar ($dom -> saveXML($node), XSD_ANYXML);
现在我的结果很好:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://pse/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns1:mi102Response>
<cdhead version="14"/>
</ns1:mi102Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
关于返回的XML的根名称-我确信我会找到一种方法来将其更改为我想要的名称(而不是由SoapVar生成的标准mi102Response)!
As for the root name of the returned XML - I am sure I will find out a way to change that to whatever I want it to be (instead of the standard mi102Response generated by SoapVar)!!
这篇关于如何在soapServer响应中返回自定义XML响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!