質問

I'm wondering if it's possible to change part of the SOAP envelope using XSLT. The reason for me to amend only part of it is because the remaining is a dynamic response.

I had below XML message:

<SOAP-ENV:Body>
      <xyz:TheResponse Status="S" xmlns:xyz="namespace">
         <Hdr>
            <Sndr>
               ...
            </Sndr>
         </Hdr>
         <Command>
            ...
         </Command>
         <Data>
            ...
         </Data>
      </xyz:TheResponse>
   </SOAP-ENV:Body>

And I'm using below XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="namespace">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/SOAP-ENV:Body">
    <abc:SomeServiceResp xmlns:abc="SomePackage.SOAP.SomeService">
    <xsl:copy-of select="*"/>
    </abc:SomeServiceResp>
    </xsl:template>
</xsl:stylesheet>

The result of the XSLT is as below:

<SOAP-ENV:Body>
    <abc:SomeServiceResp xmlns:abc="SomePackage.SOAP.SomeService" xmlns:tns="namespace">
              <xyz:TheResponse Status="S" xmlns:xyz="namespace">
                 <Hdr>
                    <Sndr>
                       ...
                    </Sndr>
                 </Hdr>
                 <Command>
                    ...
                 </Command>
                 <Data>
                    ...
                 </Data>
              </xyz:TheResponse>
    </abc:SomeServiceResp>
</SOAP-ENV:Body>

I got the expected result in which it's adding in this 1 line in my response <abc:SomeServiceResp xmlns:abc="SomePackage.SOAP.SomeService" xmlns:tns="namespace".

However, I intend to change the second line:

<xyz:TheResponse Status="S" xmlns:xyz="namespace"> into

<tns:TheResponse Status="S" xmlns:tns="namespace"> leaving the 'Status' as it is as it's a dynamic response.

Does anyone have any idea how I can work this out?

役に立ちましたか?

解決

How about changing the template to match TheReoponse and then do an explicit namespace cast like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tns="namespace1"
    xmlns:SOAP-ENV="SOAP"
    xmlns:xyz="namespace">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />


    <xsl:template match="/SOAP-ENV:Body">
      <xsl:copy>
        <abc:SomeServiceResp xmlns:abc="SomePackage.SOAP.SomeService">
          <xsl:apply-templates/>
        </abc:SomeServiceResp>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="xyz:TheResponse">

      <tns:TheResponse xmlns:tns="namespace">
        <xsl:attribute name="Status">
          <xsl:value-of select="@Status"/>
        </xsl:attribute>
        <xsl:copy-of select="*"/>
      </tns:TheResponse>

    </xsl:template>

    <xsl:template match="*">
      <xsl:copy>
        <xsl:apply-templates />
      </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top