0

I would like to replace a part of URL with XMLStarlet.

http://example.com:8081 to http://example2.com

XML:

<SHOP>
    <SHOPITEMS>
        <SHOPITEM>
            <IMGURL>http://example.com:8081/image.jpg</IMGURL>
            <IMAGES>
                <IMGURL>http://example.com:8081/image2.jpg</IMGURL>
                <IMGURL>http://example.com:8081/image3.jpg</IMGURL>
            </IMAGES>
        </SHOPITEM>
    </SHOPITEMS>
</SHOP>

As you can see the text I'd like to replace is on multiple levels in:

/SHOP/SHOPITEMS/SHOPITEM/IMGURL
/SHOP/SHOPITEMS/SHOPITEM/IMAGES/IMGURL

So far I tried:

xmlstarlet ed -u "//SHOP/SHOPITEMS/SHOPITEM/IMGURL/*[starts-with(text(), 'http://example.com:8081')]" -v http://example2.com input.xml

not worked... and:

xmlstarlet ed -u "//SHOP/SHOPITEMS/SHOPITEM/IMGURL/text()" -x "str:replace(., 'http://example.com:8081', 'http://example2.com')" input.xml

xmlXPathCompOpEval: function replace not found
Unregistered function
Segmentation fault

Any help is appreciated.

2
  • 1
    XMLStarlet uses libxslt, and since the EXSLT function str:replace() was removed from libxslt in 2013 it has been unavailable. Perhaps you could consider doing something like sed '/<IMGURL>/s/\(example\).com:8081/\12.com/' input.xml or an awk alternative like awk '/<IMGURL>/{sub("example.com:8081", "example2.com")}1' input.xml. Commented Jan 18, 2021 at 20:03
  • Why is this tagged as xslt? Commented Jan 18, 2021 at 21:08

1 Answer 1

1

Not a real replace, but a substring trick. At least it works:

xmlstarlet ed -u "//*[starts-with(text(),'http://example.com:8081/')]" -x "concat('http://example2.com/',substring(text(),25))" input.xml
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.