标签: xmlstarlet

如何使用xmlstarlet在另一个元素下插入新元素?

$ vim test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<config>
</config>
$ xmlstarlet ed -i "/config" -t elem -n "sub" -v "" test.xml
<?xml version="1.0" encoding="UTF-8"?>
<sub></sub>
<config>
</config>
Run Code Online (Sandbox Code Playgroud)

但我希望sub成为配置的孩子.我该如何更改-ixpath参数

奖励:是否可以直接使用属性插入子项,甚至将其设置为值?就像是:

$ xmlstarlet ed -i "/config" -t elem -n "sub" -v ""  -a attr -n "class" -v "com.foo" test.xml
Run Code Online (Sandbox Code Playgroud)

xml xpath command-line command xmlstarlet

25
推荐指数
3
解决办法
2万
查看次数

测试XML属性

我有一段像这样的XML:

<root>
    <foo src=""/>
    <foo src="bar"/>
    <foo />
</root>
Run Code Online (Sandbox Code Playgroud)

我想知道哪些元素具有src属性,这些属性为空且具有值.

我来的最远的是

$ xmlstarlet sel -t -m '//foo' -v @src -n foo.xml 

bar
Run Code Online (Sandbox Code Playgroud)

虽然这并没有告诉我第三个foo缺少属性.

xpath xmlstarlet

22
推荐指数
4
解决办法
3万
查看次数

XMLStarlet不会选择任何内容

我有一个典型的pom.xml,并希望打印groupId,artifactId和版本,用冒号分隔.我认为XMLStarlet是适合它的工具.我尝试了几种方法,但我总是得到一个空行.

xml sel -t -m project -v groupId -o : -v artifactId -o : -v version pom.xml
Run Code Online (Sandbox Code Playgroud)

预期产量:

org.something.apps:app-acct:5.4
Run Code Online (Sandbox Code Playgroud)

实际输出:空行

即使我尝试只打印groupId我什么也得不到:

xml sel -t -v project/groupId pom.xml
Run Code Online (Sandbox Code Playgroud)

我确信该工具可以看到这些元素,因为我可以毫无问题地列出它们:

xml el pom.xml
Run Code Online (Sandbox Code Playgroud)

打印以下(正确):

project
project/modelVersion
project/parent
project/parent/groupId
project/parent/artifactId
project/parent/version
project/groupId
project/artifactId
project/version
project/packaging
Run Code Online (Sandbox Code Playgroud)

怎么了?

这是pom.xml的简化版本:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.something</groupId>
        <artifactId>base</artifactId>
        <version>1.16</version>
    </parent>

    <groupId>org.something.apps</groupId>
    <artifactId>app-acct</artifactId>
    <version>5.4</version>
    <packaging>war</packaging>

</project>
Run Code Online (Sandbox Code Playgroud)

command-line-interface pom.xml xmlstarlet

16
推荐指数
2
解决办法
8638
查看次数

xmlstarlet选择值

这是xml-data:

<DATA VERSION="1.0">
  <TABLES>
    <ITEM>
      <identifyer V="1234"></identifyer>
      <property1 V="abcde"></property1>
      <Property2 V="qwerty"></property2>
    </ITEM>
    <ITEM>
      <identifyer V="5678"></identifyer>
      <Property1 V="zyxwv"></property1>
      <Property2 V="dvorak"></property2>
    </ITEM>
  </TABLES>
</DATA>
Run Code Online (Sandbox Code Playgroud)

我试图找到有价值property2的项目.我可以选择数据:identifyer1234

$ xmlstarlet sel -t -c "/DATA/TABLES/ITEM/identifyer [@V=1234]" test.xml 
<identifyer V="1234"/>
Run Code Online (Sandbox Code Playgroud)

需要两种类型的输出:

$ xmlstarlet <some magic>
<identifyer V="1234"></identifyer>
<property1 V="abcde"></property1>
<Property2 V="qwerty"></property2>
Run Code Online (Sandbox Code Playgroud)

和:

$ xmlstarlet <some magic>
qwerty
Run Code Online (Sandbox Code Playgroud)

xml xmlstarlet

16
推荐指数
1
解决办法
3万
查看次数

使用xmlstarlet提取和转储元素

我正在寻找一种使用xmlstarlet从我的xml中提取和打印元素的方法; 例如,如果我的xml是

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>
Run Code Online (Sandbox Code Playgroud)

我想打印出价格= 29.99的book元素:

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
Run Code Online (Sandbox Code Playgroud)

我理解选择这样一个元素的xpath查询(/bookstore/book[price=29.99)但我无法在stdout上打印/转储它.如果我使用'-v'选项并使用-v(.)我没有得到我想要的输出(包含其中的所有标签)我只是得到文本值.应该有一种简单地转储所选元素的方法,这就是我要找的东西.

谢谢你的期待.

xml xmlstarlet

15
推荐指数
1
解决办法
2万
查看次数

如何?xmlstarlet通过id提取HTML数据

我有一个简单的任务,让我把头发拉出来,我敢肯定我非常接近.

这是我的xhtml文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Test Page</title>
</head>

<body>

<p>
test
</p>

<table id="test_table">
<tr><td>test</td><td>test</td></tr>
<tr><th>mo test</th></tr>
</table>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

...和xmlstarlet喜欢它:

$ xmlstarlet.exe el -v test.xhtml
html[@xmlns='http://www.w3.org/1999/xhtml']
html/head
html/head/title
html/body
html/body/p
html/body/table[@id='test_table']
html/body/table/tr
html/body/table/tr/td
html/body/table/tr/td
html/body/table/tr
html/body/table/tr/th
Run Code Online (Sandbox Code Playgroud)

我需要做的是提取表标签中的数据,最好不要使用HTML.对此的上下文是我正在编写一个测试集,其中一个网页被调用然后写入文件.测试要求我验证表数据,但如果页面上的其他内容发生变化,则允许测试成功.此外,我不会事先知道该表将包含多少列或行,它可能会根据数据而有所不同.

但是当我尝试:

$ xmlstarlet.exe sel -t -c "/html/body/table[@id='test_table']" test.xhtml
Attempt to load network entity http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
None of the XPaths matched; to match a node in the default namespace
use '_' as the prefix (see section …
Run Code Online (Sandbox Code Playgroud)

xml xhtml xmlstarlet

12
推荐指数
1
解决办法
3858
查看次数

如何在xmlstarlet中声明XPath名称空间?

我是xmlstarlet的新手,所以希望这个答案很简单.

我正在编写一个脚本来从命令行修改Inkscape SVG文件.我选择了xmlstarlet工具.

在测试文件上测试命令语法后,我在真正的SVG文件上遇到了麻烦.我认为使用命名空间会让我失望.

示例文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->    
<svg
   xmlns:osb="http://www.openswatchbook.org/uri/2009/osb"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="603"
   height="1000"
   viewBox="0 0 159.54375 264.58334"
   version="1.1"
   id="svg8"
   inkscape:version="0.92.1 r"
   sodipodi:docname="test.svg"
   inkscape:export-filename="/home/user/dev/inkscape/test/1.png"
   inkscape:export-xdpi="96"
   inkscape:export-ydpi="96">
  <defs
     id="defs2">
    <linearGradient
       inkscape:collect="always"
       id="linearGradient6204">
      <stop
         style="stop-color:#8f1a22;stop-opacity:1;"
         offset="0"
         id="stop6200" />
      <stop
         style="stop-color:#8f1a22;stop-opacity:0;"
         offset="1"
         id="stop6202" />
    </linearGradient>
  </defs>
</svg>
Run Code Online (Sandbox Code Playgroud)

我想换Gradient6204Gradient9999.

我写了这个命令,它不起作用(只返回原始文件).

xmlstarlet ed -u "/svg/defs/linearGradient[@id='linearGradient6204']/@id" -v 'linearGradient9999' text.txt
Run Code Online (Sandbox Code Playgroud)

我也再次尝试,添加名称空间与-N但没有运气.我发现如果删除该行:

xmlns="http://www.w3.org/2000/svg"
Run Code Online (Sandbox Code Playgroud)

从文件然后我上面写的命令工作.

以我描述的方式更新上面的SVG文件的正确语法是什么?

xml xpath svg xml-namespaces xmlstarlet

10
推荐指数
1
解决办法
1303
查看次数

xmlstarlet sel输出值之间的新行

我有以下XML

<process id="test">

</process>
<process id="test2">

</process>
Run Code Online (Sandbox Code Playgroud)

我确实从属性中获取了正确的值,但我希望用新行分隔它们以进行进一步处理.

使用命令时

xmlstarlet sel -t -v "count(process/@id" "example.xml)"
Run Code Online (Sandbox Code Playgroud)

我可以看到两者是分开的,因为它确实返回2

用于检索属性的命令如下

xmlstarlet sel -t -v "process/@id" "example.xml"
Run Code Online (Sandbox Code Playgroud)

题 :

可以使用每个属性的新行返回输出吗?如果不能用xmllint完成?

xmlstarlet

8
推荐指数
1
解决办法
6394
查看次数

如何使用xmlstarlet更新xml文件

我正在使用Windows版本的xmlstarlet通过Windows批处理文件更新xml文件.

xml edit --update "/xml/table/rec[@id=3]/@id" --value 10 %xmlfile%
Run Code Online (Sandbox Code Playgroud)

我希望这会将rec节点的id属性更新为10.当我运行它时,我在命令行中看到了预期的更新的xml,但文件永远不会更新.

我怎么能这样做,我想远离重写整个文件,因为文件可能很大.

更新前:

<?xml version="1.0"?>
<xml>
  <table>
    <rec id="1" />
    <rec id="2" />
    <rec id="3" />
  </table>
</xml>
Run Code Online (Sandbox Code Playgroud)

更新后:

<?xml version="1.0"?>
<xml>
  <table>
    <rec id="1" />
    <rec id="2" />
    <rec id="10" />
  </table>
</xml>
Run Code Online (Sandbox Code Playgroud)

xml bash xmlstarlet

8
推荐指数
1
解决办法
7942
查看次数

xmlstarlet sel在大文件上

命令

$ xmlstarlet sel -t -c "/collection/record" file.xml
Run Code Online (Sandbox Code Playgroud)

在应用给定的Xpath表达式之前,似乎将整个文件加载到内存中.这不适用于大型XML文件.

是否xmlstarlet提供流式传输模式以从大型(100G +)XML文件中提取子元素?

xml xpath command-line xmlstarlet

7
推荐指数
2
解决办法
1717
查看次数