I am trying to parse an xml but before parsing it I am cleaning some unnecassary attributes from it by using Regex.Replace method of .NET.
This is my input
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:loginResponse>
<loginReturn xsi:type="xsd:string">asdsadsadasdas2321312dasdasdas21asdas</loginReturn>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is what I wrote for regex
xmlns:[a-zA-Z\d-=":\/\.]+
This is output
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:loginResponse>
<loginReturn xsi:type="xsd:string">asdsadsadasdas2321312dasdasdas21asdas</loginReturn>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Found 5 matches:
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:Magento"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
String literals for use in programs:
C#
@"xmlns:[a-zA-Z\d-="":\/\.]+"
and this is wanted output
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<ns1:loginResponse>
<loginReturn>asdsadsadasdas2321312dasdasdas21asdas</loginReturn>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I tried something like
xmlns?xsi?[a-zA-Z\d-=":\/\.]+
but it doesnt work. It also doesnt seem that can solve the attributes that begins with SOAP-ENV anyway.
Thanks.
Try this for a regex.. this might get you the output you want.
The question mark makes the previous statement optional and in your case only makes the ‘s’ and the ‘i’ optional
xmln s?xs i?[a-zA-Z\d-=”:/.]+
If you meant to make the
xmlnand thexsitags optional your regex should look like thisThis would catch alot since the regex will capture everthing in the
[a-zA-Z\d-=":\/\.]+part as well