标签: xmlstarlet

使用XSL在第一个位置插入XML节点

如果导入不存在,XSLT将当前插入导入

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    ...
    ...
    <Import Project="$(SolutionDir)BuildShared.targets" />
</Project>
Run Code Online (Sandbox Code Playgroud)

我需要它将它作为第一个节点插入

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(SolutionDir)BuildShared.targets" />
    ...
    ...
</Project>
Run Code Online (Sandbox Code Playgroud)

template.xsl;

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match='@*|node()'>
        <xsl:copy>
            <xsl:apply-templates select='@*|node()'/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/ms:Project[not(ms:Import[@Project='$(SolutionDir)BuildConfiguration.targets'])]">
        <xsl:copy>          
            <xsl:apply-templates select="node()|@*"/>
            <Import xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Project="$(SolutionDir)BuildConfiguration.targets"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

交换导入和apply-templates行给出;

runtime error: file template.xsl line 9 element copy

Attribute nodes must be added before any child nodes to an element.

xml xslt xmlstarlet

4
推荐指数
1
解决办法
1917
查看次数

xmlstarlet更新值没有任何反应

我有一个xml文件

<?xml version="1.0"?>
<preferences>
    <!--General options-->
    <options>
            <dbHost>localhost</dbHost>
            <dbUser>bwserver</dbUser>
            <dbPass>bwserver</dbPass>
            <dbPort>3306</dbPort>
Run Code Online (Sandbox Code Playgroud)

我如何更新值dbUser?

当我输入

xmlstarlet edit --update '/preferences/options/dbUser/'  --value 123 preferences.xml
Run Code Online (Sandbox Code Playgroud)

什么都没发生.我只看到终端中的文件内容.没有触及xml文件.

xml bash xmlstarlet

4
推荐指数
1
解决办法
4376
查看次数

如何使用XmlStarlet将具有属性的元素插入XML文件?

源XML

<xml>
    <block>
        <el name="a">92346</el>
        <el name="b">lorem</el>
    </block>
    <block>
        <el name="a">89753</el>
        <el name="b">ipsum</el>
    </block>
</xml>
Run Code Online (Sandbox Code Playgroud)

宾语

我想<el name="c">0</el>在每个<block>Linux shell脚本中插入一个元素:

<xml>
    <block>
        <el name="a">92346</el>
        <el name="b">lorem</el>
        <el name="c">0</el>
    </block>
    <block>
        <el name="a">89753</el>
        <el name="b">ipsum</el>
        <el name="c">0</el>
    </block>
</xml>
Run Code Online (Sandbox Code Playgroud)

我可以使用XmlStarlet附加元素:

xmlstarlet ed -a '/xml/block/el[@name="b"]' \
              --type 'elem' -n 'el' -v 0
Run Code Online (Sandbox Code Playgroud)

问题

  1. 选择每个<el>没有name属性的元素的XPath表达式是什么?
  2. 我可以附加元素并使用单个xml ed命令插入属性 吗?

xml shell xpath xmlstarlet

4
推荐指数
1
解决办法
5768
查看次数

如何在xmlstarlet XPath中使用XML命名空间?

目前我正在编辑XML文件.当我写命令

xml ed -u "/project/version" -v "2.7.13-NEW-SNAPSHOT" pom.xml > ./pom_new.xml
Run Code Online (Sandbox Code Playgroud)

它写了新的xml文件,但是当我打开文件时,nothings在其中发生了变化.

下面是给定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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.groupID.test</groupId>
    <artifactId>test-api-parent-pom</artifactId>
    <version>2.7.13-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>test-api-parent-pom</name>
    ...
    ...
</project>
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

xml xpath element edit xmlstarlet

4
推荐指数
1
解决办法
529
查看次数

XMLStarlet:每个项目打印一行,同时使用父元素的数据

我有以这种方式格式化的 XML 数据:

<XML>
    <Waveforms Time="01/01/2009 3:00:02 AM">
        <WaveformData Channel="I">1, 2, 3, 4, 5, 6 </WaveformData>
        <WaveformData Channel="II">9, 8, 7, 6, 5, 4 </WaveformData>
    </Waveforms>
    <Waveforms Time="01/01/2009 3:00:04 AM">
        <WaveformData Channel="I">1, 2, 3, 4, 5, 6 </WaveformData>
        <WaveformData Channel="II">9, 8, 7, 6, 5, 4 </WaveformData>
    </Waveforms>
</XML>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 xmlstarlet 将此数据解析为文本文件(逗号分隔)。所需的输出如下所示:

Time Attribute, Channel Attribute, Data
01/01/2009 3:00:02 AM, I, 1, 2, 3, 4, 5, 6
01/01/2009 3:00:02 AM, II, 9, 8, 7, 6, 5, 4
01/01/2009 3:00:02 AM, I, 1, …
Run Code Online (Sandbox Code Playgroud)

xml bash xpath parsing xmlstarlet

4
推荐指数
1
解决办法
865
查看次数

通过 XMLStarlet 取消 &amp; 符号 (&amp;) - Bugging &amp;

这是一个相当烦人但更简单的任务。根据本指南,我写了这个:

#!/bin/bash

content=$(wget "https://example.com/" -O -)
ampersand=$(echo '\&')

xmllint --html --xpath '//*[@id="table"]/tbody' - <<<"$content" 2>/dev/null |
    xmlstarlet sel -t \
        -m "/tbody/tr/td" \
            -o "https://example.com" \
            -v "a//@href" \
            -o "/?A=1" \
            -o "$ampersand" \
            -o "B=2" -n \
Run Code Online (Sandbox Code Playgroud)

我成功地提取从表中的每个环节和一切都被正确地连接在一起,然而,而不是再现符号我收到这在每一个环节的结尾:

https://example.com/hello-world/?A=1\&amp;B=2
Run Code Online (Sandbox Code Playgroud)

但实际上,我正在寻找类似的东西:

https://example.com/hello-world/?A=1&B=2
Run Code Online (Sandbox Code Playgroud)

这个想法是使用反斜杠转义字符,\&以便它被忽略。最初,在这种情况下,我尝试将其直接放入-o "\&" \而不是-o "$ampersand" \删除ampersand=$(echo '\&')。结果还是一样。

本质上,通过删除反斜杠它仍然输出:

https://example.com/hello-world/?A=1&amp;B=2
Run Code Online (Sandbox Code Playgroud)

只是\后面的&amp;被删除了。

为什么?

我敢肯定这是缺少的基本知识。

bash shell escaping wget xmlstarlet

4
推荐指数
1
解决办法
923
查看次数

xmlstarlet 替换 xml 节点值

我是新使用 xmlstarlet 的。我想知道如何使用 xmlstarlet 更改 xml 节点的值。

我尝试了一些东西。xmlstarlet ed --inplace -u '/file_input/uri' 'string("s3://my_source")' template.xml > output.xml

不起作用。

我的预期输出为 s3://my_sources3://mydestination。我想更改source_pathdestination_path节点。

<?xml version="1.0" encoding="UTF-8"?>
<job version="2.10.8">
  <input>
    <deblock_enable>Auto</deblock_enable>
    <deblock_strength>0</deblock_strength>
    <no_psi>false</no_psi>
    <order>1</order>
    <timecode_source>zerobased</timecode_source>
    <file_input>
      <certificate_file nil="true"/>
      <password>upass</password>
      <uri>source_path</uri>
      <username>uname</username>
    </file_input>
    <file_group_settings>
      <rollover_interval nil="true"/>
      <destination>
        <password>upass</password>
        <username>uname</username>
        <uri>destination_path</uri>
      </destination>
    </file_group_settings>
  </input>
</job>
Run Code Online (Sandbox Code Playgroud)

非常感谢

xmlstarlet

4
推荐指数
1
解决办法
7884
查看次数

为什么 xmlstarlet 说没有“ends-with”函数?

我正在使用xmlstarletchangeSetliquibase XML 变更日志中提取viewName以“v”结尾的节点。

然而,xmlstarlet 抱怨ends-withXPATH 函数不存在:

$ xmlstarlet sel -N x="http://www.liquibase.org/xml/ns/dbchangelog" -t -m \
"/x:databaseChangeLog/x:changeSet[x:createView[ends-with(@viewName, 'v')]]" \
-c . public.db.changelog.xml

xmlXPathCompOpEval: function ends-with not found
Unregistered function
Stack usage errror
xmlXPathCompiledEval: 3 objects left on the stack.
runtime error: element for-each
Failed to evaluate the 'select' expression.
None of the XPaths matched; to match a node in the default namespace
use '_' as the prefix (see section 5.1 in the manual).
For …
Run Code Online (Sandbox Code Playgroud)

xpath xmlstarlet

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

AWS S3 ListBucketResult分页没有身份验证?

我正在寻找一个公共S3存储桶中所有对象的简单清单。

我知道如何获得最多包含1000个结果的curl列表,尽管我不知道如何对结果进行分页才能获得完整列表。我认为标记是一个线索。

我不想使用SDK /库或进行身份验证。我正在寻找几行外壳程序来执行此操作。

shell pagination amazon-s3 xmlstarlet

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

使用 xmlstarlet 选择多个元素

由于我在其他地方找不到解释的示例,因此我以问答形式分享我的发现。

考虑这个学生名单(pupils.xml):

<pupils>
    <pupil>
        <firstName>Adam</firstName>
        <lastName>Amith</lastName>
        <birthDate>2000-01-01</birthDate>
    </pupil>
    <pupil>
        <firstName>Berta</firstName>
        <lastName>Bmith</lastName>
        <birthDate>2000-01-02</birthDate>
    </pupil>
    <pupil>
        <firstName>Caesar</firstName>
        <lastName>Cmith</lastName>
        <birthDate>2000-01-03</birthDate>
    </pupil>
    <pupil>
        <firstName>Doris</firstName>
        <lastName>Dmith</lastName>
        <birthDate>2000-01-04</birthDate>
    </pupil>
</pupils>
Run Code Online (Sandbox Code Playgroud)

我如何选择每个学生的两个子属性以获得如下列表:

Adam Amith
Berta Bmith
Caesar Cmith
Doris Dmith
Run Code Online (Sandbox Code Playgroud)

xml xmlstarlet

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

标签 统计

xmlstarlet ×10

xml ×6

xpath ×4

bash ×3

shell ×3

amazon-s3 ×1

edit ×1

element ×1

escaping ×1

pagination ×1

parsing ×1

wget ×1

xslt ×1