-2

I have an XML file and I want to replace every uppercase word with a code (example -x! Where x is the number of uppercase words).

This is my xml:

<a att="HELLO">
  <b int="Ten">
    <c time="0">Lorem IPSUM dolor sit AMET,</c>
    <c time="10">consectetuer ADIPISCING elit.</c>
  </b>
  <b int="Twenty">
    <c time="20">Aenean COMMODO ligula EGET dolor.</c>
  </b>
  <b int="Forty">
    <c time="25">Aenean massa. </c>
    <c time="30">CUM sociis natoque penatibus et MAGNIS dis parturient montes,</c>
    <c time="40">nascetur ridiculus mus.</c>
   </b>
Etc... 
</a>

Now, what I want is pass the string to a function and return a xml like this:

<a att="HELLO">
  <b int="Ten">
    <c time="0">Lorem -0! dolor sit -1!,</c>
    <c time="10">consectetuer -2! elit.</c>
  </b>
  <b int="Twenty">
    <c time="20">Aenean -3! ligula -4! dolor.</c>
  </b>
  <b int="Forty">
    <c time="25">Aenean massa. </c>
    <c time="30">-5! sociis natoque penatibus et -6! dis parturient montes,</c>
    <c time="40">nascetur ridiculus mus.</c>
   </b>
Etc... 
</a>

I have to store all the uppercase words in an array because at the end I want another function to replace every code (-x!) with is respective word.

I'm not at home right now so I can't show the code that I tried (I will update the question with code as soon as possible)

  • Create a `parser = new DOMParser();`, And feed it your `xmlStr`: `myXML = parser.parseFromString(xmlStr, "application/xml");`. Recursively traverse the `myXML` tree using `childNodes` and check if `nodeType==3` => text to work on, else call the recursive function again. On the texts, you can use @mplungjan's line: `currentNode.textContent = currentNode.textContent.replace(/(\b[A-Z]+\b)/g,function(match) { matches.push(match); return `-${cnt++}!`});`, remembering to `cnt = 0, matches = [];` right after setting `myXML` and before the recursive call. – iAmOren Oct 31 '20 at 11:01

1 Answers1

0

this works with test if inside tag :

let str = `<a att="HELLO">
  <b int="Ten">
    <c time="0">Lorem IPSUM dolor sit AMET,</c>
    <c time="10">consectetuer ADIPISCING elit.</c>
  </b>
  <b int="Twenty">
    <c time="20">Aenean COMMODO ligula EGET dolor.</c>
  </b>
  <b int="Forty">
    <c time="25">Aenean massa. </c>
    <c time="30">CUM sociis natoque penatibus et MAGNIS dis parturient montes,</c>
    <c time="40">nascetur ridiculus mus.</c>
   </b>
</a>`;
let i = 0;
str = str.replace(/(?<=>.*)(\b[A-Z]+\b)/g, () => {
  return `-${i++}!`;
});
console.log(str);
mplungjan
  • 134,906
  • 25
  • 152
  • 209
aziz k'h
  • 743
  • 2
  • 10