标签: xmlstarlet

为什么xmlstarlet没有选择所有节点?

从w3schools考虑这个例子:

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

<bookstore>

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>

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

xmlstarlet sel -t -v"// @ category"books.xml

仅返回COOKING.如何获取所有属性命名类别?

奖励问题:我如何评估XML文件的XPath查询,而无需在编辑器或程序中打开它(我想要一个快速工具).问题是将大文件加载到内存会导致编辑器崩溃.不要告诉我eXist是我最好的选择吗?

xpath command-line-interface xmlstarlet exist-db

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

使用xmlstarlet通过id删除SVG中的节点

我有一个包含许多SVG的文件夹.在所有SVG中,我需要删除具有特定ID的标记.有太多的文件可以手工完成,所以我一直在研究一个shell脚本为我做这个,但是无法弄清楚如何使用xmlstarlet来删除标签.

例如,假设我们有foo.svg:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
  <g id="delete"/>
  <g id="keep"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

然后发出命令: xmlstarlet ed -d "g[@id='delete']" foo.svg > bar.svg

会导致:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
  <g id="keep"/>
</svg>
Run Code Online (Sandbox Code Playgroud)

,但事实并非如此.bar.foo仍然包含<g id="delete"/>.

有谁知道删除<g id="delete"/>标签的正确命令?或者我可以使用任何其他工具来批量处理SVG文件来摆脱不需要的标签?

干杯,多米尼克

shell svg xmlstarlet

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

Bash脚本使用XMLStarlet将xml元素解析为key = value对

我有这个xml文件(filename:myFile.xml)和用户的数据:

<?xml version="1.0" encoding="utf-8"?>
<params>
    <username>jDoe</username>
    <password>abc123</password>
    <firstname>John</firstname>
    <lastname>Doe</lastname>
    <email>jdoe@example.com</email>
    <country>Germany</country>
</params>
Run Code Online (Sandbox Code Playgroud)

我可以在我的bash脚本中打开它并使用"for"循环来迭代它的内容:

for i in $(xmlstarlet select -t -v '/params/*' myFile.xml)
do
   echo $i
done
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到:

jDoe
abc123
John
Doe
jdoe@example.com
Germany
Run Code Online (Sandbox Code Playgroud)

如何将每个值与其相对名称相关联,并创建一个bash脚本变量,如下所示:

username="jDoe"
password="abc123"
firstname="John"
lastname="Doe"
email="jdoe@example.com"
country="Germany"
Run Code Online (Sandbox Code Playgroud)

换句话说,对于每个标记,我想读取它的名称和它的值,然后创建一个bash变量.即:

tagname="value"
Run Code Online (Sandbox Code Playgroud)

我更喜欢循环遍历标签,因为它们比这个例子更多,并不总是相同.

有什么建议?

xml bash sh xmlstarlet

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

为什么这个 XPath 表达式在 xmlstarlet 中没有返回正确的值?

我正在尝试使用 xmlstarlet 提取此 KML 文件中“坐标”节点的内容。

KML 文件使用 xmlstarlet 本身可以很好地验证。

我将其缩减为一个小测试文件,其中包含:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
    <name>eurovelo-5 690</name>
    <Snippet></Snippet>
    <description><![CDATA[&nbsp;]]></description>
    <styleUrl>#style390</styleUrl>
    <LineString>
      <tessellate>1</tessellate>
      <coordinates>
        10.146948,44.790592,97.500000
        10.146958,44.790562,97.599998
        10.147018,44.790497,97.699997
        10.147083,44.790466,97.699997
      </coordinates>
    </LineString>
  </Placemark>
  </Document>
</kml>
Run Code Online (Sandbox Code Playgroud)

但运行此查询失败:

xmlstarlet sel -t -c "//coordinates/text()" test.kml
Run Code Online (Sandbox Code Playgroud)

这似乎使用在线路径工具正确解析 - http://www.qutoric.com/xslt/analysisr/xpathtool.html

我在这里错过了什么吗?

xml xpath kml xmlstarlet

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

bash+xmlstarlet:如何索引到列表或填充数组?

我尝试使用 xmlstarlet 从以下示例 XML 中选择单个节点:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="key.xsl" ?>
<tables>
  <tableset>
    <table name="table1">
      <row>
        <fld name="fileName">
          <strval><![CDATA[/my/XYZ/file1]]></strval>
        </fld>
        <fld name="fileName">
          <strval><![CDATA[/my/XYZ/file2]]></strval>
        </fld>
        <fld name="fileName">
          <strval><![CDATA[/my/other/XYZ/file3]]></strval>
        </fld>
        <fld name="worksBecauseUnique">
          <strval><![CDATA[/XYZ/unique]]></strval>
        </fld>
      </row>
    </table>
  </tableset>
</tables>
Run Code Online (Sandbox Code Playgroud)

我正在尝试在 bash 中构建关联数组...如何选择单个节点,或使用 xmlstarlet 迭代多个节点?

到目前为止,我正在尝试类似以下的方法,但它不起作用:

xmlstarlet sel -t -v "//tables/tableset/table/row/fld[@name=\"fileName\"]/strval[0]" xmlfile.xml
Run Code Online (Sandbox Code Playgroud)

希望得到“/my/XYZ/file1”,但这不起作用。

bash xmlstarlet

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

如何在Linux中安装/设置XMLStarlet?

我对Linux和Shell脚本还很陌生。并且需要解析和查询xml。我能够成功找到并使用Windows的XML starlet。

但是,我的目标是通过外壳程序脚本在Linux上运行它。

谁能分享在Linux上安装和配置XMLStarlet的步骤?

谢谢!

xml linux shell xmlstarlet xml-parsing

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

xmlstarlet:如果属性不存在则创建该属性,否则对其进行编辑

我想使用 xmlstarlet 查找包含属性 inkscape:label="L2" 的 xml 文件的元素,并将其属性“style”设置为值“display.inline”。困难在于属性“样式”可能已经定义,也可能尚未定义。

我目前正在使用这个命令:

xmlstarlet edit --inplace --update "//*[@inkscape:label=\"L2\"]/@style" --value "display:inline" ex.svg 
Run Code Online (Sandbox Code Playgroud)

如果已经定义了属性样式,它将起作用

// It works on this
<g inkscape:groupmode="layer"
 id="layer2"
 inkscape:label="L2"
 style="display:none">
Run Code Online (Sandbox Code Playgroud)

但它不会工作,否则:

// Does not work
<g inkscape:groupmode="layer"
 id="layer2"
 inkscape:label="L2">
Run Code Online (Sandbox Code Playgroud)

我还定义了一个可以添加所需属性的命令:

xmlstarlet ed --insert "//*[@inkscape:label=\"L2\"]" --type attr -n style -v "display:inline" ex.svg > output.svg
Run Code Online (Sandbox Code Playgroud)

不幸的是,如果该属性已经存在,则会添加第二个:

// The element now contains two attributes style
<g inkscape:groupmode="layer"
 id="layer2" 
 inkscape:label="L2" 
 style="display:none" 
 style="display:inline">
Run Code Online (Sandbox Code Playgroud)

如果属性不存在,有没有办法创建它,否则可以编辑它?

xml xpath attributes xmlstarlet

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

使用 xmlstarlet 替换 XML 中的标记值

我试图testlookupinput.xml替换该值。脚本正在执行,没有任何错误,但它没有替换该值。

sed -i '/<Filter>/,/<\/Filter>/s/test/lookup/' input.xml > input_1.xml
Run Code Online (Sandbox Code Playgroud)

下面是我的 input.xml 文件:

<NikuDataBus
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./src/webroot/WEB-INF/xog/xsd/nikuxog_entity.xsd">
    <Header version="14.2" action="read" objectType="contentPack" externalSource="NIKU"/>
    <LookupQuery>
        <Filter name="code" criteria="EQUALS">test</Filter>
    </LookupQuery>
</NikuDataBus>
Run Code Online (Sandbox Code Playgroud)

xml shell sed xmlstarlet

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

Bash - 在文件中搜索特定字符串并替换为直接源

我试图在包含以下内容的文件中搜索字符串"missing":

<message>
    <source>TypeA</source>
    <translation>missing</translation>
</message>
<message>
    <source>TypeB</source>
    <translation>missing</translation>
</message>
<message>
    <source>TypeC</source>
    <comment>Context menu</comment>
    <translation>missing</translation>
</message>
Run Code Online (Sandbox Code Playgroud)

如果找到"缺失",我想用它的直接源名称替换字符串.例如:

<message>
    <source>TypeA</source>
    <translation>TypeA</translation>
</message>
<message>
    <source>TypeB</source>
    <translation>TypeB</translation>
</message>
<message>
    <source>TypeC</source>
    <comment>Context menu</comment>
    <translation>TypeC</translation>
</message>
Run Code Online (Sandbox Code Playgroud)

到目前为止,我能够使用awk搜索字符串并打印直接源名称:

match($0, /<source>(.*)<\/source>/,n){ src=n[1] }
match($0, /<translation>(.*)<\/translation>/,s){ trs=s[1] }
/unfinished/{ print "Translation missing or incomplete for: '" trs "'","located inside source named: '" src "'" }
Run Code Online (Sandbox Code Playgroud)

然后将其保存为something.awk使用以下方式调用:

awk -f something.awk filelocation
Run Code Online (Sandbox Code Playgroud)

但我不知道如何用源中的值替换字符串"missing".

任何人都可以建议我如何更换它?

bash awk sed xmlstarlet

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

Bash - 如果子节点的属性值不等于特定值,则删除 XML 节点?

我有 RSS 提要,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>my feed</title>
  <link rel="self" href="http://myhomesite.com/articles/feed/"/>
  <updated>2019-11-04T12:45:00Z</updated>
  <id>http://myhomesite.com/articles/feed/?dt=2019-11-04T12:45:00Z</id>
  <entry>
    <id>id0</id>
    <link rel="alternate" type="text/html" href="https://yandex.ru/link123"/>
    <author>
      <name/>
    </author>
    <published>2019-11-04T12:45:00Z</published>
    <updated>2019-11-04T12:45:00Z</updated>
    <title type="html"><![CDATA[foo bar foo bar]]></title>
    <content type="html"><![CDATA[]]></content>
  </entry>
  <entry>
    <id>id2</id>
    <link rel="alternate" type="text/html" href="https://myhomesite.com"/>
    <author>
      <name/>
    </author>
    <published>2019-11-04T09:45:00Z</published>
    <updated>2019-11-04T09:45:00Z</updated>
    <title type="html"><![CDATA[foo bar foo bar]]></title>
    <content type="html"><![CDATA[]]></content>
  </entry>
....
Run Code Online (Sandbox Code Playgroud)

我想删除link href !=/feed/entry处的所有节点 ( ) 。 http://myhomesite.com

如何使用 Bash 删除值从指定符号开始的 XML 节点?

xml linux bash xmlstarlet

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

标签 统计

xmlstarlet ×10

xml ×6

bash ×4

shell ×3

xpath ×3

linux ×2

sed ×2

attributes ×1

awk ×1

command-line-interface ×1

exist-db ×1

kml ×1

sh ×1

svg ×1

xml-parsing ×1