繁体   English   中英

如何将 SOAP 标头添加到 soap 消息 - XSLT

[英]How to add SOAP Headers to soap message- XSLT

我收到带有空标题的 SOAP 请求。 我想向传入的请求添加自定义标头并将 soap 正文复制到响应中。如何在标头中添加 xml 标记? 以下是传入的请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
   <soapenv:Body>
      <input>
         <numb>15171</numb>
      </input>
   </soapenv:Body>
</soapenv:Envelope>

预期 Output:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header>
          <Username xmlns="http://blah.com">username</Username>
          <Password xmlns="http://blah.com">password</Password>
       </soapenv:Header>
       <soapenv:Body>
          <input>
             <numb>15171</numb>
          </input>
       </soapenv:Body>
    </soapenv:Envelope>

这是我试过的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   
    xmlns:soapenv="http://soap/envelope/"
    version="1.0">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  
       <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="soapenv:Header">
    
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <soapenv:Header>
      <Username xmlns="http://blah.com">username</Username>
          <Password xmlns="http://blah.com">password</Password>
  
        </soapenv:Header>
    </xsl:copy>
</xsl:template>

<xsl:template match="soapenv:Header"/>
</xsl:stylesheet>

这是你可以做到的。 请注意,XSLT 中的“soapenv”命名空间与输入 XML 中定义的名称不匹配。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    version="1.0">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="soapenv:Header">
    <xsl:copy>
      <Username xmlns="http://blah.com">username</Username>
      <Password xmlns="http://blah.com">password</Password>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

看到它在这里工作: https://xsltfiddle.liberty-development.net/6q1SDkU

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM