将 XML 中的 URL 部分替换为 xmlstarlet

Replace part of URL in XML with xmlstarlet

我想用 XMLStarlet 替换 URL 的一部分。

http://example.com:8081http://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>

如您所见,我要替换的文本位于多个级别:

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

到目前为止我尝试了:

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

无效...并且:

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

感谢任何帮助。

不是真正的替换,而是子字符串技巧。至少它有效:

xmlstarlet ed -u "//*[starts-with(text(),'http://example.com:8081/')]" -x "concat('http://example2.com/',substring(text(),25))" input.xml