0

I have a xml document with some repeating tags. Simple example goes like:

<dog>
  <cat>
...
<dog>
   <cat> 
...
<dog>
   <cat>

A sample regex for this example is <dog>\s+<cat> which matches each tag combo. That's what I initially want. My question is if I wanted to do substitution (capture group and back reference) but DON'T want to replace each matching pattern with the same value how do I specify? Like in the example above what if I wanted to replace each tag combo with something like:

<monkey>
   <cat>
...
<elephant>
   <cat>
...
<lion>
   <cat>

I guess in general is there a way to make a substitution non-global? I believe I read that without global substitution it'll only replace the 1st matching pattern. I could use that technique to replace each tag one by one but wanted to know if there was a better solution. The goal is to use the linux sed command but I've been testing in Notepad++.

Seapoe
  • 459
  • 4
  • 15
  • 2
    please use xml parser like xmlstarlet instead of sed/regex... – Sundeep Jun 13 '18 at 02:10
  • Imagine you have the following input: ` 1 2 3 – Allan Jun 13 '18 at 02:52
  • You can use the following stylesheet: ` ` – Allan Jun 13 '18 at 02:54
  • After taking a backup of your input file run: `animals=( monkey elephant lion ); for ani in "${animals[@]}"; do java -jar saxon9he.jar -s:animals.xml -xsl:animalsConvertor.xs lt -o:animals.xml animalName="$ani"; done` output: ` 1 2 3 ` – Allan Jun 13 '18 at 02:57
  • For more info on `saxon` https://www.saxonica.com/documentation9.5/using-xsl/commandline.html and on XSLT: https://www.w3schools.com/xml/xsl_elementref.asp – Allan Jun 13 '18 at 02:57

0 Answers0