Solving ID Reference Problem in SOAP Message
In some .net services, the request or response can sometimes differ from what they actually want.
With this process, this request message is edited and sent.
Example Input Value
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://sicil.webservices2.vedop2.ggm.gov.tr" xmlns:types="http://sicil.webservices2.vedop2.ggm.gov.tr/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:centerInformationDetailTCKN>
<tckn xsi:type="xsd:string">xxx</tckn>
<userInfo href="#id1"/>
</tns:centerInformationDetailTCKN>
<q1:WSInfo id="id1" xsi:type="q1:WSInfo" xmlns:q1="http://webservices2.vedop2.ggm.gov.tr">
<loginCode xsi:nil="true"/>
<loginPassword xsi:nil="true"/>
<requestTime xsi:nil="true"/>
<userIP xsi:nil="true"/>
<userCode>xx</userCode>
<orgName>xxxx</orgName>
<macAddress xsi:nil="true"/>
</q1:WSInfo>
</soap:Body>
</soap:Envelope>
XML
XSLT Conversion Code
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/encoding/" >
<xsl:key name="multiref-by-id" match="q1:WSInfo" xmlns:q1="http://webserviceaddress.com" use="@id"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="@*|*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(@href, '#')]">
<xsl:copy>
<xsl:apply-templates select="@* |
key('multiref-by-id', substring-after(@href, '#'))/@* |
key('multiref-by-id', substring-after(@href, '#'))/node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@href[starts-with(., '#')] | q1:WSInfo[@id] | @soapenv:root" xmlns:q1="http://webserviceaddress.com" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML
Example Output Value
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://xAddress.com" xmlns:types="http://xAddress.com/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<tns:aMethodName>
<tckn xsi:type="xsd:string">xxx</tckn>
<userInfo id="id1" xsi:type="q1:WSInfo">
<loginCode xmlns:q1="http://webserviceaddress.com" xsi:nil="true"/>
<userIP xmlns:q1="http://webserviceaddress.com" xsi:nil="true"/>
<userCode xmlns:q1="http://webserviceaddress.com">xx</userCode>
<orgName xmlns:q1="http://webserviceaddress.com">xxxx</orgName>
<macAddress xmlns:q1="http://webserviceaddress.com" xsi:nil="true"/>
</userInfo>
</tns:aMethodName>
</soap:Body>
</soap:Envelope>
XML
Similarly, if you want to do the same without a namespace, the following example can be used:
Example Input Value
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:mainMethodNameQueryResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://someAddress.com">
<mainMethodNameQueryReturn href="#id0"/>
</ns1:mainMethodNameQueryResponse >
<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:aMethodNameInfo" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://someAddress.com">
<xTable soapenc:arrayType="ns2:anotherMethodNameInfo[1]" xsi:type="soapenc:Array">
<xTable href="#id1"/>
</xTable>
<yTable soapenc:arrayType="ns2:anotherMethodNameInfo[1]" xsi:type="soapenc:Array">
<yTable href="#id2"/>
</yTable>
<unreferencedOneField xsi:type="ns2:unreferencedOneField" xsi:nil="true"/>
</multiRef>
<multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns4:anotherMethodNameInfo" xmlns:ns4="http://someAddress.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<description xsi:type="xsd:string">Some explanations</description>
<value xsi:type="xsd:string">1033.65</value>
<type xsi:type="xsd:string">4</type>
<title xsi:type="xsd:string" xsi:nil="true"/>
</multiRef>
<multiRef id="id2" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns5:anotherMethodNameInfo" xmlns:ns5="http://someAddress.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<description xsi:type="xsd:string">Some explanations</description >
<value xsi:type="xsd:string">361.21</value >
<type xsi:type="xsd:string">3</type >
<title xsi:type="xsd:string" xsi:nil="title"/>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
XML
XSLT Conversion Code
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/encoding/">
<xsl:key name="multiref-by-id" match="multiRef" use="@id"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="@*|*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(@href, '#')]">
<xsl:copy>
<xsl:apply-templates select="@* |
key('multiref-by-id', substring-after(@href, '#'))/@* |
key('multiref-by-id', substring-after(@href, '#'))/node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@href[starts-with(., '#')] | multiRef[@id] | @soapenv:root"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML
Example Output Value
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:mainMethodNameQueryResponse xmlns:ns1="http://someAddress.com" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<mainMethodNameQueryReturn id="id0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:aMethodNameInfo">
<xTable xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://someAddress.com" soapenc:arrayType="ns2:anotherMethodNameInfo[1]" xsi:type="soapenc:Array">
<xTable id="id1" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns4:anotherMethodNameInfo">
<description xmlns:ns4="http://someAddress.com" xsi:type="xsd:string">Some explanations</description>
<value xmlns:ns4="http://someAddress.com" xsi:type="xsd:string">1033.65</value>
<type xmlns:ns4="http://someAddress.com" xsi:type="xsd:string">4</type>
<title xmlns:ns4="http://someAddress.com" xsi:type="xsd:string" xsi:nil="true"/>
</xTable>
</xTable>
<yTable xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://someAddress.com" soapenc:arrayType="ns2:anotherMethodNameInfo[1]" xsi:type="soapenc:Array">
<yTable id="id2" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns5:anotherMethodNameInfo">
<description xmlns:ns5="http://someAddress.com" xsi:type="xsd:string">Some explanations</description>
<value xmlns:ns5="http://someAddress.com" xsi:type="xsd:string">361.21</value>
<type xmlns:ns5="http://someAddress.com" xsi:type="xsd:string">3</type>
<title xmlns:ns5="http://someAddress.com" xsi:type="xsd:string" xsi:nil="true"/>
</yTable>
</yTable>
<unreferencedOneField xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://someAddress.com" xsi:type="ns2:unreferencedOneField" xsi:nil="true"/>
</mainMethodNameQueryReturn>
</ns1:mainMethodNameQueryResponse<
</soapenv:Body>
</soapenv:Envelope>
XML