修改SOAP信封

时间:2014-12-31 22:10:36

标签: xml xslt soap peoplesoft

我对XSLT相当新,并想知道如何更改XML SOAP消息以在其间添加更多标记

源XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <DataValidationFailureFault xmlns="http://sample.com">
         <ValidationErrorList>
            <ValidationError>
               <ErrorCode>1234</ErrorCode>
               <ErrorString>Test Error</ErrorString>
            </ValidationError>
         </ValidationErrorList>
      </DataValidationFailureFault>
   </soapenv:Body>
</soapenv:Envelope>

在XSLT之后,我希望XML SOAP看起来像:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>HardCoded Value</faultcode>
         <faultstring>HardCoded Value</faultstring>
         <detail>
            <DataValidationFailureFault xmlns="http://sample.com">
               <ValidationErrorList>
                  <ValidationError>
                    <ErrorCode>1234</ErrorCode>
                    <ErrorString>Test Error</ErrorString>
                  </ValidationError>
               </ValidationErrorList>
            </DataValidationFailureFault>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

2 个答案:

答案 0 :(得分:0)

尝试这样的事情

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ex="http://sample.com">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/*">
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <soapenv:Body>
        <soapenv:Fault>
          <faultcode>HardCoded Value</faultcode>
          <faultstring>HardCoded Value</faultstring>
          <xsl:copy-of select="soapenv:Body/ex:DataValidationFailureFault"/>
        </soapenv:Fault>
      </soapenv:Body>
    </soapenv:Envelope>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

identity tranform开头。然后,编写另一个模板,在正确的位置干预身份复制过程,即处理soapenv:Body元素时。

XSLT样式表

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

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="soapenv:Body">
    <xsl:copy>
      <soapenv:Fault>
          <faultcode>HardCoded Value</faultcode>
          <faultstring>HardCoded Value</faultstring>
          <detail>
              <xsl:apply-templates select="@*|node()"/>
          </detail>
      </soapenv:Fault>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

XML输出

顺便说一句,确定你需要所有那些未使用的命名空间声明吗?

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <soapenv:Fault>
         <faultcode>HardCoded Value</faultcode>
         <faultstring>HardCoded Value</faultstring>
         <detail>
            <DataValidationFailureFault xmlns="http://sample.com">
               <ValidationErrorList>
                  <ValidationError>
                     <ErrorCode>1234</ErrorCode>
                     <ErrorString>Test Error</ErrorString>
                  </ValidationError>
               </ValidationErrorList>
            </DataValidationFailureFault>
         </detail>
      </soapenv:Fault>
   </soapenv:Body>
</soapenv:Envelope>

修改

  

好奇我如何删除DataValidationFailureFault标记中的命名空间?

在这种情况下,将另一个模板添加到样式表中,该模板匹配该命名空间中的元素(DataValidationFailureFault及其后代 - 它是默认命名空间)。然后,为每个元素构造一个没有命名空间的新元素:

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

并修改XSLT代码的stylesheet元素以包含sample:前缀的名称空间声明,并且还排除此前缀,因为它在输出中未使用。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://sample.com"
exclude-result-prefixes="sample">