XSLT Soap Message

2019-09-14 14:03发布

问题:

I can't change the name of tags in a SOAP response. I saw a lot of postings about it, but I did not find an applicable solution.

My original XML is:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
   <soap:Header>
      <MessageID xmlns="http://www.w3.org/2005/08/addressing">uuid:4d6b87d8-fe14-4579-ac34-fe841c184a4b</MessageID>
      <RelatesTo RelationshipType="Reply" xmlns="http://www.w3.org/2005/08/addressing">uuid:1f9b0c7e-f36c-4fa3-ac2b-2377b57b6634</RelatesTo>
      <Action xmlns="http://www.w3.org/2005/08/addressing">http://xxx</Action>
   </soap:Header>
   <soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <OP1 xmlns="http://xxx/">
         <OPR>
            <OPO>
               <Cod>..</Cod>
               <A1>hi my...</A1>

            </OPO>
         </OPR>
      </OP1>
   </soap:Body>
</soap:Envelope>

I want to change A1 for ANAME.

My xsl is

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">

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

   <xsl:template match="A1">
        <ANAME><xsl:apply-templates /></ANAME>
   </xsl:template>      
</xsl:stylesheet>

Thanks!

回答1:

Just add a named namespace declaration for your namespace "http://xxx/" to your stylesheet element like this:

xmlns:aaa="http://xxx/"

Then your can match the A1 elements with aaa:A1 in your template:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:aaa="http://xxx/">

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

   <xsl:template match="aaa:A1">
     <xsl:element name="ANAME" namespace="http://xxx/">
       <xsl:apply-templates />
     </xsl:element>
   </xsl:template>     

</xsl:stylesheet>

Partial output:

...
<soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <OP1 xmlns="http://xxx/">
        <OPR>
            <OPO>
                <Cod>..</Cod>
                <ANAME>hi my...</ANAME>

            </OPO>
        </OPR>
    </OP1>
</soap:Body>


标签: xslt soap