添加命名空间和 header 的 XSL 转换(输入和输出描述)

XSL transformation to add namespace and header (input & output described)

XSL 转换初学者,寻求使用 xsl 转换数据的帮助。

输入数据如下:

<?xml version="1.0" encoding="UTF-8"?>
 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
   <abcRequest xmlns="http://google.com/2018/abcService">
     <messageHeader>
        <Id>000000</Id>
        <aId>572b0285-7e06-4834-90c0-dc45eeeafe70</aId>
        <version>1.0</version>
     </messageHeader>
   </abcRequest>
  </soap:Body>
</soap:Envelope>

预期输出数据如下:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:p="http://www.bnppf.com/20190101/TA/TA99eWLNotificationDataSrv">
  <soap:Header>
    <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.gmail.com/20190101/newService</wsa:Action>
  </soap:Header>
  <soap:Body>
    <p:abcRequest xmlns="http://google.com/2018/abcService">
     <p:messageHeader>
        <p:Id>000000</p:Id>
        <p:aId>572b0285-7e06-4834-90c0-dc45eeeafe70</p:aId>
        <p:version>1.0</p:version>
     </p:messageHeader>
    </p:abcRequest>
  </soap:Body>
</soap:Envelope>

您可以使用以下样式表实现转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:abc="http://google.com/2018/abcService" xmlns:p="http://www.bnppf.com/20190101/TA/TA99eWLNotificationDataSrv">
  <xsl:output method="xml"/>

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

  <xsl:template match="soap:Envelope">
    <soap:Envelope>
        <soap:Header>
          <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.gmail.com/20190101/newService</wsa:Action>
        </soap:Header>
        <xsl:apply-templates select="soap:Body" />
    </soap:Envelope>
  </xsl:template>

  <xsl:template match="abc:*">
      <xsl:element name="p:{local-name()}">
          <xsl:apply-templates select="node()|@*" />
      </xsl:element>
  </xsl:template> 

</xsl:stylesheet>

但是您的 p:abcRequest 元素上的以下默认 xmlns 命名空间将不会创建,因为它是多余的。

<p:abcRequest xmlns="http://google.com/2018/abcService">

相反,将为该元素及其子元素设置 xmlns:p 命名空间。
输出为:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:abc="http://google.com/2018/abcService" xmlns:p="http://www.bnppf.com/20190101/TA/TA99eWLNotificationDataSrv">
    <soap:Header>
        <wsa:Action xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.gmail.com/20190101/newService</wsa:Action>
    </soap:Header>
    <soap:Body>
        <p:abcRequest>
            <p:messageHeader>
                <p:Id>000000</p:Id>
                <p:aId>572b0285-7e06-4834-90c0-dc45eeeafe70</p:aId>
                <p:version>1.0</p:version>
            </p:messageHeader>
        </p:abcRequest>
    </soap:Body>
</soap:Envelope>