XSL转换以添加名称空间和标头(描述了输入和输出)

时间:2019-03-05 19:33:59

标签: xslt xslt-1.0 xslt-grouping

开始进行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>

1 个答案:

答案 0 :(得分:0)

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

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

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

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