How to add multiple instances of a hreflang tag throughout sitemap.xml

Using TextWrangler, I am trying to add an hreflang tag to each URL throughout a large sitemap.

Is there a script that can add:

<xhtml:link rel=“alternate” hreflang=“en-ca” href=“https://mywebsite.com/en_ca/” />
<xhtml:link rel=“alternate” hreflang=“en-us” href=“https://mywebsite.com/en_us/” />

After each instance in the document there is the URL tag, as seen in the below example:

https://mywebsite.com/en_ca/product-1/ 2019-09-19 daily 0.8

And at the same time pull in the subfolder (/product-1/) from the URL found in the tag.

The goal would be to have each url tag look like this after:

https://mywebsite.com/en_ca/product-1/ 2019-09-19 daily 0.8

Essentially:

  1. Input hreflang tag after tag
  2. Grab everything after /en_ca/ in the tag and add that after /en_ca/ and /en_us/ in the added hreflang tag as well.

I’m looking for anyone who knows of script to accomplish this, or can tell me if it is even possible to do.

Thank you.