简体   繁体   中英

How to add SOAP Headers to soap message- XSLT

I'm getting the SOAP request with empty Headers. I want to add custom headers to the incoming request and copy the soap body as is to the response.How to add xml tags in the Headers? Below is the incoming request:

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

Expected 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>

Here is my XSLT which I tried:

<?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>

Here's how you could do it. Note that your 'soapenv' namespace in your XSLT was not matching what is defined in your input 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>

See it working here: https://xsltfiddle.liberty-development.net/6q1SDkU

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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