removing namespace aliases from xml -


i need remove namespace aliases xml. xml being received backend service (different responses can have different structures) , needs converted json format finally. hence i'm looking generic xslt remove namespace aliases xml before convert jsonx , json.

xml have:

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">    <soapenv:body>       <ns4:policysnapshotresponse xmlns="http://www.aig.com/acord1/xml/" xmlns:ns4="http://www.aig.com/gct/services/policyinquiryservicev1.0" xmlns:ns2="http://www.acord.org/standards/pc_surety/acord1/xml/" xmlns:ns3="http://www.aig.com/gct/services/commonheaderv1.0">          <ns4:requestheader>             <ns3:id>spoofy</ns3:id>             <ns3:requestapplicationid/>             <ns3:requestmessageid/>             <ns3:echoback>false</ns3:echoback>          </ns4:requestheader>          <ns4:applicationcontext>             <ns3:businesssegment>cl</ns3:businesssegment>             <ns3:region>all</ns3:region>             <ns3:knowledgedate>2015-05-24</ns3:knowledgedate>             <ns3:country>ie</ns3:country>             <ns3:language>en</ns3:language>             <ns3:lineofbusiness>aig:causc</ns3:lineofbusiness>             <ns3:sublineofbusiness>autop</ns3:sublineofbusiness>             <ns3:systemdate>2015-06-03</ns3:systemdate>             <ns3:targetsystemname>goald</ns3:targetsystemname>          </ns4:applicationcontext>          <ns4:policyinqrs>             <ns2:polinfo>                <ns2:commlpropertypolicy>                   <ns2:commlpolicy/>                </ns2:commlpropertypolicy>             </ns2:polinfo>          </ns4:policyinqrs>       </ns4:policysnapshotresponse>    </soapenv:body> </soapenv:envelope> 

xml need without namespacesand soap envelope:

<policysnapshotresponse>        <requestheader>           <id>spoofy</id>           <requestapplicationid/>           <requestmessageid/>           <echoback>false</echoback>        </requestheader>        <applicationcontext>           <businesssegment>cl</businesssegment>           <region>all</region>           <knowledgedate>2015-05-24</knowledgedate>           <country>ie</country>           <language>en</language>           <lineofbusiness>aig:causc</lineofbusiness>           <sublineofbusiness>autop</sublineofbusiness>           <systemdate>2015-06-03</systemdate>           <targetsystemname>goald</targetsystemname>        </applicationcontext>        <policyinqrs>           <polinfo>              <commlpropertypolicy>                 <commlpolicy/>              </commlpropertypolicy>           </polinfo>        </policyinqrs>     </policysnapshotresponse> 

i converting ths xml further jsonx , json.

try this

using system; using system.collections.generic; using system.linq; using system.text; using system.xml; using system.xml.linq; using system.text.regularexpressions;  namespace consoleapplication1 {     class program     {         static void main(string[] args)         {             string input =                  "<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +                    "<soapenv:body>" +                       "<ns4:policysnapshotresponse xmlns=\"http://www.aig.com/acord1/xml/\" xmlns:ns4=\"http://www.aig.com/gct/services/policyinquiryservicev1.0\" xmlns:ns2=\"http://www.acord.org/standards/pc_surety/acord1/xml/\" xmlns:ns3=\"http://www.aig.com/gct/services/commonheaderv1.0\">" +                          "<ns4:requestheader>" +                             "<ns3:id>spoofy</ns3:id>" +                             "<ns3:requestapplicationid/>" +                             "<ns3:requestmessageid/>" +                             "<ns3:echoback>false</ns3:echoback>" +                          "</ns4:requestheader>" +                          "<ns4:applicationcontext>" +                             "<ns3:businesssegment>cl</ns3:businesssegment>" +                             "<ns3:region>all</ns3:region>" +                             "<ns3:knowledgedate>2015-05-24</ns3:knowledgedate>" +                             "<ns3:country>ie</ns3:country>" +                             "<ns3:language>en</ns3:language>" +                             "<ns3:lineofbusiness>aig:causc</ns3:lineofbusiness>" +                             "<ns3:sublineofbusiness>autop</ns3:sublineofbusiness>" +                             "<ns3:systemdate>2015-06-03</ns3:systemdate>" +                             "<ns3:targetsystemname>goald</ns3:targetsystemname>" +                          "</ns4:applicationcontext>" +                          "<ns4:policyinqrs>" +                             "<ns2:polinfo>" +                                "<ns2:commlpropertypolicy>" +                                   "<ns2:commlpolicy/>" +                                "</ns2:commlpropertypolicy>" +                             "</ns2:polinfo>" +                          "</ns4:policyinqrs>" +                       "</ns4:policysnapshotresponse>" +                    "</soapenv:body>" +                 "</soapenv:envelope>";              string pattern = "(</?)([^:]*:)";             regex expr = new regex(pattern);             input = expr.replace(input, "$1");              xdocument doc = xdocument.parse(input);             xelement element = doc.descendants().where(x => x.name.localname == "policysnapshotresponse").firstordefault();             foreach (xattribute attribute in element.attributes().tolist())             {                 attribute.remove();             }             doc = xdocument.parse(element.tostring());          }     } } ​ 

Comments