Last active
September 30, 2019 05:18
-
-
Save MaryamZi/3c9259c1a630dcbc6dbc13f458bee83a to your computer and use it in GitHub Desktop.
An example demonstrating how to remove all attributes of a Ballerina XML value recursively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ballerina/io; | |
public function main() { | |
xmlns "http://abc.def"; | |
xml x = xml `<hello foo="bar"> | |
<ns1:world xmlns:ns1="http://qwe.asd" xyz="pqr">hello world</ns1:world> | |
</hello>`; | |
io:println(x); | |
// <hello xmlns="http://abc.def" foo="bar"> | |
// <ns1:world xmlns:ns1="http://qwe.asd" xyz="pqr">hello world</ns1:world> | |
// </hello> | |
io:println("\nRemoving Attributes..."); | |
removeAttributes(x); | |
io:println(x); | |
// Removing Attributes... | |
// <hello xmlns="http://abc.def"> | |
// <ns1:world xmlns:ns1="http://qwe.asd">hello world</ns1:world> | |
// </hello> | |
} | |
function removeAttributes(xml x) { | |
if !x.isElement() { | |
return; | |
} | |
x.setAttributes({}); | |
foreach xml|string y in x.getChildren() { | |
if (y is xml) { | |
removeAttributes(y); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment