使用XSLT将标签插入soap标头

时间:2009-08-26 03:47:03

标签: xml xslt

我正在处理一个soap响应文件,我们的要求是在请求期间将响应中捕获的某些数据添加到响应中。我在这里有这个xml响应,就像使用XSLT文件将某些数据添加到它的标题部分一样。请指教。

实际回应

<soap:Envelope xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
      <wsse:Security>
         <wsu:Timestamp wsu:Id="Timestamp-7cd6d5e5">
            <wsu:Created>2009-08-26</wsu:Created>
         </wsu:Timestamp>
      </wsse:Security>
   </soap:Header>
   <soap:Body>
      <GetProxy>
         <ProxiesList/>
      </GetProxy>
   </soap:Body>
</soap:Envelope> 

需要xslt将其转换为

<soap:Envelope xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header>
      <wsse:Security>
         <wsu:Timestamp wsu:Id="Timestamp-7cd6d5e5">
            <wsu:Created>2009-08-26</wsu:Created>
         </wsu:Timestamp>
      </wsse:Security>
       <ut:reqCode xmlns:ut="temp.org">
       <ut:reqInfo>information from request</ut:reqInfo>
       </ut:reqCode>
   </soap:Header>
   <soap:Body>
      <GetProxy>
         <ProxiesList/>
      </GetProxy>
   </soap:Body>
</soap:Envelope>

感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:3)

将内容插入XML的有用模式是使用identity transform复制所有内容,并为要更改的标记覆盖它:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                version="1.0">
  <xsl:output method="xml"
              indent="yes"/>

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

  <!-- special handling for soap:Header -->
  <xsl:template match="soap:Header">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>

      <!-- insert the following inside the soap:Header tag -->
      <ut:reqCode xmlns:ut="temp.org">
        <ut:reqInfo>information from request</ut:reqInfo>
      </ut:reqCode>

    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

这基本上只是复制了所有内容,但是在复制其内容后soap:Header添加了一些其他内容。