如何将XML作为另一个XML中的内部文本添加,作为SOAP请求的参数

问题描述 投票:0回答:1

示例,我想将XML作为XML SOAP中的参数发送:

   <soapenv:Header/>
   <soapenv:Body>
      <doc:makeSomething>
         <param1>blah blah</param1>
         <pamar2>3.14159</param2>
         <xml>
                 <!-- this is what i want to insert -->
                 <myDOC>
                     <tag1>xxx</tag1>
                     <tag2>yyy</tag2>
                     .....
                     .....
                 </myDOC>                 
         </xml>
      </doc:makeSomething>
   </soapenv:Body>
</soapenv:Envelope>

我该怎么办?使用urlencode?谢谢

xml soap encode urlencode
1个回答
0
投票

您可以简单地使用XSLT-1.0的fn:document(...)功能。因此,请使用带有以下文件的XSLT处理器在输入XML中的所需位置插入insert.xml。以下XML

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:doc="http://some.doc">
    <soapenv:Header/>
    <soapenv:Body>
        <doc:makeSomething>
            <param1>blah blah</param1>
            <param2>3.14159</param2>
            <xml>
                <!-- this is what i want to insert -->
                <myDOC>
                    <tag1>xxx</tag1>
                    <tag2>yyy</tag2>
                    .....
                    .....
                </myDOC>
            </xml>
        </doc:makeSomething>
    </soapenv:Body>
</soapenv:Envelope>

以及要插入的名为insert.xml的XML文件

<Items>   
 <Item id='1'/>
 <Item id='3'/>
 <Item id='5'/>
 <Item id='7'/>
</Items>

使用此XSLT-1.0样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:doc="http://some.doc" version="1.0"> 
    <xsl:output method="xml" indent="yes" version="1.0" encoding="utf-8" />

    <!-- Identity template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/soapenv:Envelope/soapenv:Body/doc:makeSomething/xml">
        <xsl:copy>
            <xsl:copy-of select="document('insert.xml')"/>
            <xsl:copy-of select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

将产生以下输出:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope/" xmlns:doc="http://some.doc">
    <soapenv:Header/>
    <soapenv:Body>
        <doc:makeSomething>
            <param1>blah blah</param1>
            <param2>3.14159</param2>
            <xml>
                <!-- Here the file 'insert.xml' is inserted -->
                <Items>
                    <Item id="1"/>
                    <Item id="3"/>
                    <Item id="5"/>
                    <Item id="7"/>
                </Items>
                <!-- Here the already present part is copied -->
                <myDOC>
                    <tag1>xxx</tag1>
                    <tag2>yyy</tag2>
                    .....
                    .....
                </myDOC>
            </xml>
        </doc:makeSomething>
    </soapenv:Body>
</soapenv:Envelope>

这是XSLT所做的。这是完成所需任务的最简单方法之一。

© www.soinside.com 2019 - 2024. All rights reserved.