XMLStarlet ( http://xmlstar.sourceforge.net/overview.php ) được viết bằng C và sử dụng libxml2và libxslt.
Đưa ra tài liệu XML
<?xml version="1.0"?>
<root>
<tag>data</tag>
</root>
một mã con rootcó thể được chèn bằng cách sử dụng
xml ed -s '/root' -t elem -n 'newtag' -v 'newdata' file.xml
sản xuất
<?xml version="1.0"?>
<root>
<tag>data</tag>
<newtag>newdata</newtag>
</root>
Chèn nhiều thứ (sử dụng bản gốc file.xmlở trên cùng ở đây):
xml ed -s '/root' -t elem -n 'newtag' \
-s '/root/newtag' -t elem -n 'subtag' -v 'subdata' file.xml
Điều này tạo ra
<?xml version="1.0"?>
<root>
<tag>data</tag>
<newtag>
<subtag>subdata</subtag>
</newtag>
</root>
Ví dụ trong câu hỏi:
xml ed -N x="http://maven.apache.org/POM/4.0.0" \
-s '/x:project' -t elem -n 'distributionManagement' \
-s '/x:project/distributionManagement' -t elem -n 'repository' \
-s '/x:project/distributionManagement/repository' -t elem -n 'id' \
-v 'private-releases' \
-s '/x:project/distributionManagement/repository' -t elem -n 'url' \
-v 'https://my.private.server.com/nexus/repository/maven-releases/' \
file.xml
Kết quả:
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- a lot of other tags-->
<distributionManagement>
<repository>
<id>private-releases</id>
<url>https://my.private.server.com/nexus/repository/maven-releases/</url>
</repository>
</distributionManagement>
</project>
Chèn một tệp XML đã chuẩn bị trước đó tại một vị trí trong XML:
Giả sử XML gốc từ câu hỏi được đặt vào file.xmlvà các bit bổ sung sẽ có trong distributinManagementnút mới nằm trong new.xml(chứ không phải chính thẻ nút), người ta có thể thực hiện các thao tác sau để chèn new.xmlvào nút gốc:
xml ed -N x="http://maven.apache.org/POM/4.0.0" \
-s '/x:project' -t elem -n 'distributionManagement' \
-v "$(<new.xml)" file.xml | xml unesc | xml fo
XMLStarlet sẽ tự động thoát dữ liệu cần thoát, chẳng hạn như <và các >ký tự. Các xml unescchút unescapes dữ liệu chèn (nó thực sự unescapes toàn bộ tài liệu, mà có thể hoặc không thể là một vấn đề), và xml fođịnh dạng lại kết quả tài liệu XML.
Kết quả là
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- a lot of other tags-->
<distributionManagement>
<repository>
<id>private-releases</id>
<url>https://my.private.server.com/nexus/repository/maven-releases/</url>
</repository>
</distributionManagement>
</project>
Tôi hơi khó chịu khi làm theo cách này, "nhưng nó hoạt động".
Xem thêm câu hỏi liên quan này trên StackOverflow: /programming/29298507/xmlstarlet-xinclude-xslt