Skip to content

Instantly share code, notes, and snippets.

@MaryamZi
Last active September 30, 2019 05:18
Show Gist options
  • Save MaryamZi/3c9259c1a630dcbc6dbc13f458bee83a to your computer and use it in GitHub Desktop.
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.
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