XML 到 SOAP 的转换

Posted

技术标签:

【中文标题】XML 到 SOAP 的转换【英文标题】:XML to SOAP transformation 【发布时间】:2011-05-31 03:40:44 【问题描述】:

我是 XML、XSLT 和 SOAP 的新手,我想知道是否可以转换这个 XML 文件

<?xml version="1.0" encoding="UTF-8"?>

<SEARCHREQUEST>

    <PSSSEARCHPARAM1>Database name</PSSSEARCHPARAM1>
    <PSSSEARCHPARAM2>Description</PSSSEARCHPARAM2>
    <PSSSEARCHPARAM3>Document number</PSSSEARCHPARAM3>
    <PSSSEARCHPARAM4>Belong To</PSSSEARCHPARAM4>

</SEARCHREQUEST>

进入这个 SOAP 请求

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header/>
<soap:Body>

    <wor:SearchDocuments xmlns:wor="http://worksite.imanage.com">

        <wor:Databases>
            <wor:string>Database name</wor:string>
        </wor:Databases>

        <wor:ProfileSearchParameters>

            <wor:ProfileSearchParameter>    
                <wor:AttributeID>imProfileDescription</wor:AttributeID>
                <wor:SearchValue>Description</wor:SearchValue>
            </wor:ProfileSearchParameter>

            <wor:ProfileSearchParameter>
                <wor:AttributeID>imProfileCustom3</wor:AttributeID>
                <wor:SearchValue>Belong To</wor:SearchValue>
            </wor:ProfileSearchParameter>

            <wor:ProfileSearchParameter>
                <wor:AttributeID>imProfileCustom4</wor:AttributeID>
                <wor:SearchValue>APP, 20</wor:SearchValue>
            </wor:ProfileSearchParameter>

            <wor:ProfileSearchParameter>
                <wor:AttributeID>imProfileDocNum</wor:AttributeID>
                <wor:SearchValue>Document number</wor:SearchValue>
            </wor:ProfileSearchParameter>

        </wor:ProfileSearchParameters>

        <wor:SearchEmail>imSearchDocumentsOnly</wor:SearchEmail>

        <wor:OutputMask>Profile</wor:OutputMask>

        <wor:OutputProfile>

            <!-- Displays the document number-->
            <wor:imProfileAttributeID>imProfileDocNum</wor:imProfileAttributeID>

            <!-- Displays the document description/title-->
            <wor:imProfileAttributeID>imProfileDescription</wor:imProfileAttributeID>

            <!--Displays the document version-->
            <wor:imProfileAttributeID>imProfileVersion</wor:imProfileAttributeID>

            <!--Displays the standard id-->
            <wor:imProfileAttributeID>imProfileCustom16</wor:imProfileAttributeID>

            <!--Display the "Belong to" field-->
            <wor:imProfileAttributeID>imProfileCustom3</wor:imProfileAttributeID>

            <!--Displays the database name-->
            <wor:imProfileAttributeID>imProfileDatabase</wor:imProfileAttributeID>

            <!--Displays the document extension-->
            <wor:imProfileAttributeID>imProfileExtension</wor:imProfileAttributeID>

        </wor:OutputProfile>
    </wor:SearchDocuments>
</soap:Body>
</soap:Envelope>

仅使用 XSLT。如果可能的话,你能否指出一些例子来说明如何做到这一点。 Michael Kay 的“XSLT 2.0 and XPath 2.0 Programmer's Reference (4th ed.)”有很多关于如何将 XML 转换为 html 的示例,但没有关于 XML 到 SOAP 转换的示例。我能找到的最接近的东西在这里

http://wiki.netbeans.org/TransformingSOAPMessagesWithXSLT

它显示了如何转换 SOAP 请求,这不是我需要的。提前感谢您的帮助。

【问题讨论】:

我觉得应该很简单,只需要在transform中声明正确的命名空间即可。您是否只是在搜索生成 SOP 请求的转换,然后从输入 PSSSEARCHPARAM 中获取一些值? 这正是我想要做的。 在我的回答中,我将向您展示如何从您的搜索请求中获取值并将其放入各种 ProfileSearchParameter 【参考方案1】:

所以,或者你的问题真的很简单,或者我遗漏了一些明显的东西......你在寻找这样的东西吗?

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/SEARCHREQUEST">
        <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
            <soap:Header/>
            <soap:Body>

                <wor:SearchDocuments xmlns:wor="http://worksite.imanage.com">

                    <wor:Databases>
                        <wor:string><xsl:value-of select="PSSSEARCHPARAM1"/></wor:string>
                    </wor:Databases>

                    <wor:ProfileSearchParameters>

                        <wor:ProfileSearchParameter>    
                            <wor:AttributeID>imProfileDescription</wor:AttributeID>
                            <wor:SearchValue><xsl:value-of select="PSSSEARCHPARAM2"/></wor:SearchValue>
                        </wor:ProfileSearchParameter>

                        <wor:ProfileSearchParameter>
                            <wor:AttributeID>imProfileCustom3</wor:AttributeID>
                            <wor:SearchValue><xsl:value-of select="PSSSEARCHPARAM4"/></wor:SearchValue>
                        </wor:ProfileSearchParameter>

                        <wor:ProfileSearchParameter>
                            <wor:AttributeID>imProfileCustom4</wor:AttributeID>
                            <wor:SearchValue>APP, 20</wor:SearchValue>
                        </wor:ProfileSearchParameter>

                        <wor:ProfileSearchParameter>
                            <wor:AttributeID>imProfileDocNum</wor:AttributeID>
                            <wor:SearchValue><xsl:value-of select="PSSSEARCHPARAM3"/></wor:SearchValue>
                        </wor:ProfileSearchParameter>

                    </wor:ProfileSearchParameters>

                    <wor:SearchEmail>imSearchDocumentsOnly</wor:SearchEmail>

                    <wor:OutputMask>Profile</wor:OutputMask>

                    <wor:OutputProfile>

                        <!-- Displays the document number-->
                        <wor:imProfileAttributeID>imProfileDocNum</wor:imProfileAttributeID>

                        <!-- Displays the document description/title-->
                        <wor:imProfileAttributeID>imProfileDescription</wor:imProfileAttributeID>

                        <!--Displays the document version-->
                        <wor:imProfileAttributeID>imProfileVersion</wor:imProfileAttributeID>

                        <!--Displays the standard id-->
                        <wor:imProfileAttributeID>imProfileCustom16</wor:imProfileAttributeID>

                        <!--Display the "Belong to" field-->
                        <wor:imProfileAttributeID>imProfileCustom3</wor:imProfileAttributeID>

                        <!--Displays the database name-->
                        <wor:imProfileAttributeID>imProfileDatabase</wor:imProfileAttributeID>

                        <!--Displays the document extension-->
                        <wor:imProfileAttributeID>imProfileExtension</wor:imProfileAttributeID>

                    </wor:OutputProfile>
                </wor:SearchDocuments>
            </soap:Body>
        </soap:Envelope>        
    </xsl:template>

</xsl:stylesheet>

【讨论】:

谢谢。看来我的问题太简单了。 我正在使用 Mule ESB,在服务调用之后,我在 SOAPUI 中返回了一个 SOAP 响应,但令人惊讶的是,它没有 SOAP 信封和正文标签!然而,tomcat 控制台(我的服务在本地测试服务器上运行)以正确的格式提供完整的 SOAP 有效负载。我将尝试使用此方法添加那些信封和正文标签。

以上是关于XML 到 SOAP 的转换的主要内容,如果未能解决你的问题,请参考以下文章

XSLT转换需要添加SOAP信封并在SOAP标头和正文之间拆分XML

将 XML 转换为 .net 中的对象

xml字符串和java实体类相互转换JaxbXmlUtil工具类 附java实体类生成soap接口报文案例

如何将xml doc作为字节数组发送到soap服务?

如果标记不存在,则使用XSLT将XML标记添加到SOAP消息

GraphQL 的 SOAP 转换