标签: xmlstarlet

如何使用XMLStarlet在另一个元素之后直接插入元素?

使用此示例XML:

<rootnode>
  <element-a />
  <element-b />
  <element-d />
  <element-e />
</rootnode>
Run Code Online (Sandbox Code Playgroud)

如何使用XMLStarlet <element-c/>在元素后直接插入元素<element-b/>

xmlstarlet

5
推荐指数
1
解决办法
4154
查看次数

XSLT Xml 输出正在从结束斜杠中去除空格

XSLT XML 输出格式正在去除结束标记之前的空格

<Import Include="System.Web" />变成<Import Include="System.Web"/>

由于 XSLT 还删除了它所应用的文档中的许多节点,因此我想删除空格,但右斜杠除外。

xslt 被应用于许多 xml ms proj 文件

模板.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:strip-space elements="*"/>
    <!--<xsl:preserve-space elements="text"/>-->

    <xsl:template match='@*|node()'>
        <xsl:copy>
            <xsl:apply-templates select='@*|node()'/>
        </xsl:copy>
    </xsl:template>
    ... 
    ...
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

xml xslt xmlstarlet

5
推荐指数
1
解决办法
610
查看次数

xmlstarlet 格式

我正在尝试使用 xmlstarlet 格式化 xml 文件,但我不想创建新的 xml 文件。

我试过这个

xmlstarlet fo --inplace --indent-tab --omit-decl project_00.xml
Run Code Online (Sandbox Code Playgroud)

--inplace但 (format) 命令不允许使用该参数fo

有谁知道我该怎么做?

xmlstarlet

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

在xmlstarlet中按文本值选择节点

我试图提取'Value'节点的值,其中'Key'节点在bash shell中是'state':

<FrontendStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" serializerVersion="1.1">
<script/>
 <State>
  <String>
   ...
   ...
  </String>
  <String>
   ...
   ...
  </String>
  <String>
   <Key>state</Key>
   <Value>WatchingLiveTV</Value>
  </String>
  <String>
   <Key>studiolevels</Key>
   <Value>1</Value>
  </String>
  <String>
   ...
   ...
  </String>
  <String>
   ...
   ...
  </String>
 </State>
</FrontendStatus>
Run Code Online (Sandbox Code Playgroud)

如果我直接引用节点,我可以提取值:

$ xmlstarlet sel -t -m '/FrontendStatus[1]/State[1]/String[31]' -v Value <status.xml
WatchingLiveTV
Run Code Online (Sandbox Code Playgroud)

但我想通过'Key'节点的值来选择它

xml bash xmlstarlet

5
推荐指数
1
解决办法
7600
查看次数

如何过滤 XMLStarlet 中的多个属性?

例如,给定:

<fruit>
   <banana source='Ecuador' category='I'>
      <quantity>1</quantity>
   </banana>
   <banana source='Costa Rica' category='I'>
      <quantity>1</quantity>
   </banana>
</fruit>
Run Code Online (Sandbox Code Playgroud)

说我想改变

<banana source='Costa Rica' category='I'> 
Run Code Online (Sandbox Code Playgroud)

<banana source='Costa Rica' category='II'> 
Run Code Online (Sandbox Code Playgroud)

或者它的数量为 2,如果我想过滤源和初始类别值,我将如何引用它?


我正在尝试执行以下操作:

xmlstarlet ed -u "/fruit/banana[@source='Ecuador' @category='I']/quantity" -v 2
Run Code Online (Sandbox Code Playgroud)

...但这会导致语法错误,如下所示:

Invalid predicate: /fruit/banana[@source='Ecuador' @category='I']/quantity
Invalid expression: /fruit/banana[@source='Ecuador' @category='I']/quantity
Run Code Online (Sandbox Code Playgroud)

xml bash xpath xmlstarlet

5
推荐指数
1
解决办法
1403
查看次数

Bash将子节点插入XML文件

我试图使用bash编辑配置文件.我的文件看起来像这样:

<configuration>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我想<property>在文件中添加另外两个块.由于所有property标签都包含在configuration标签内,因此文件如下所示:

<configuration>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
  <property>
    <name></name>
    <value></value>
  </property>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我遇到了这个帖子并按照接受的答案,但是没有任何内容附加到我的文件中,我尝试追加的xml块是"echo-ed"作为单行字符串.我的bash文件如下所示:

file=/path/to/file/oozie-site.xml
content="<property>\n<name></name>\n<value></value>\n</property>\n<property>\n<name></name>\n<value></value>\n</property>"
echo $content
C=$(echo $content | sed 's/\//\\\//g')
sed "/<\/configuration>/ s/.*/${C}\n&/" $file
Run Code Online (Sandbox Code Playgroud)

xml bash shell sed xmlstarlet

5
推荐指数
1
解决办法
2420
查看次数

解析xml并替换特定标签shell脚本

对于下面的xml,我需要更换<studentStatus><studentName>CLASSA</studentName><studentStatus>failed</studentStatus>.

<studentFile>
    <student>
        <studentName>CLASSA</studentName>
        <studentStatus>Success</studentStatus>
        <studentActions>
            <studentAction>
                <studentType>Juniour</studentType>
                <studentStatus>Completed</studentStatus>
                <studentMsg/>
            </studentAction>
            <studentAction>
                <studentType>HighSchool</studentType>
                <studentStatus>Completed</studentStatus>
                <studentMsg/>
            </studentAction>
        </studentActions>
    </student>
    <student>
        <studentName>CLASSB</studentName>
        <studentStatus>Success</studentStatus>
        <studentActions>
            <studentAction>
                <studentType>Senior</studentType>
                <studentStatus>Completed</studentStatus>
            </studentAction>
            <studentAction>
                <studentType>Middle</studentType>
                <studentStatus>Completed</studentStatus>
            </studentAction>                         
        </studentActions>
    </student>
</studentFile>
Run Code Online (Sandbox Code Playgroud)

到目前为止我得到了什么,

xmllint -xpath "/studentFile/student[studentName='CLASSA']/studentActions/studentAction[studentType="Juniour"]/studentStatus" myxml.xml
Run Code Online (Sandbox Code Playgroud)

现在我将学生的状态视为已完成,现在此值应更改为失败.仅限于<studentType>Juniour</studentType>.我应该如何编辑xml以获得它,

<studentFile>
    <student>
        <studentName>CLASSA</studentName>
        <studentStatus>Success</studentStatus>
        <studentActions>
            <studentAction>
                <studentType>Juniour</studentType>
                <studentStatus>Failed</studentStatus>
                <studentMsg/>
            </studentAction>
            <studentAction>
                <studentType>HighSchool</studentType>
                <studentStatus>Completed</studentStatus>
                <studentMsg/>
            </studentAction>
        </studentActions>
    </student>
    <student>
        <studentName>CLASSB</studentName>
        <studentStatus>Success</studentStatus>
        <studentActions>
            <studentAction>
                <studentType>Senior</studentType>
                <studentStatus>Completed</studentStatus>
            </studentAction>
            <studentAction>
                <studentType>Middle</studentType>
                <studentStatus>Completed</studentStatus>
            </studentAction>                         
        </studentActions>
    </student>
</studentFile>
Run Code Online (Sandbox Code Playgroud)

可以使用sed完成.我知道有像xsltproc这样的工具但不确定它是否安装在我们集群的所有节点中. …

bash shell xmlstarlet xml-parsing xmllint

5
推荐指数
1
解决办法
3406
查看次数

如何使用 xmlstarlet 附加具有多个子节点的 xml 文件?

我想在之后添加这个块</audio_selector>

<input_clipping>
<end_timecode>00:00:05:00</end_timecode>        
 <order>1</order>
 <order>2</order>   
<start_timecode>00:00:01:00</start_timecode>        
</input_clipping>
Run Code Online (Sandbox Code Playgroud)

以下是我的预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<job href="/jobs/35932"  version="2.10.0.44452">
  <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>xxx</password>
      <uri>s3_source</uri>
      <username>xxx</username>
    </file_input>
    <name>input_1</name>
    <video_selector>
      <color_space>follow</color_space>
      <order>1</order>
      <program_id nil="true"/>
      <name>input_1_video_selector_0</name>
    </video_selector>
    <audio_selector>
      <default_selection>true</default_selection>
      <infer_external_filename>false</infer_external_filename>
      <order>1</order>
      <program_selection>1</program_selection>
      <selector_type>track</selector_type>
      <track>1, 2</track>
      <unwrap_smpte337>true</unwrap_smpte337>
      <name>input_1_audio_selector_0</name>
    </audio_selector>
    <input_clipping>    
    <end_timecode>00:00:05:00</end_timecode>        
     <order>1</order>
     <order>2</order>   
    <start_timecode>00:00:01:00</start_timecode>        
    </input_clipping>
  </input>
  <timecode_config>
    <require_initial_timecode>false</require_initial_timecode>
    <source>zerobased</source>
    <sync_threshold nil="true"/>
  </timecode_config>
  <ad_trigger>scte35_splice_insert</ad_trigger>
  <ad_avail_offset>0</ad_avail_offset>
  <priority>100</priority>
  <user_data></user_data>
  <avsync_enable>true</avsync_enable>
  <avsync_pad_trim_audio>true</avsync_pad_trim_audio>
  <stream_assembly>
    <name>stream_assembly_0</name>
    <video_description>
      <afd_signaling>None</afd_signaling>
      <anti_alias>true</anti_alias>
      <drop_frame_timecode>true</drop_frame_timecode>
      <fixed_afd nil="true"/>
      <force_cpu_encode>false</force_cpu_encode>
      <height>1080</height>
      <insert_color_metadata>false</insert_color_metadata>
      <respond_to_afd>None</respond_to_afd>
      <sharpness>50</sharpness>
      <stretch_to_output>false</stretch_to_output> …
Run Code Online (Sandbox Code Playgroud)

xmlstarlet

5
推荐指数
1
解决办法
1780
查看次数

使用XMLStarlet插入1000多个节点和属性-运行缓慢

这是效率问题,而不是故障排除。我有以下代码片段:

# The -R flag restores malformed XML
xmlstarlet -q fo -R <<<"$xml_content" | \
    # Delete xml_data
    xmlstarlet ed -d "$xml_data" | \
    # Delete index
    xmlstarlet ed -d "$xml_index" | \
    # Delete specific objects
    xmlstarlet ed -d "$xml_nodes/objects" | \
    # Append new node
    xmlstarlet ed -s "$xml_nodes" -t elem -n subnode -v "Hello World" | \
        # Add x attribute to node
        xmlstarlet ed -i "($xml_nodes)[last()]" -t attr -n x -v "0" | \
        # Add …
Run Code Online (Sandbox Code Playgroud)

xml unix bash shell xmlstarlet

5
推荐指数
1
解决办法
457
查看次数

如何在 BASH 中使用 xmlstarlet 计算 XML 文档中的元素数量?

我需要计算一个元素在 XML 文档中出现的次数。我需要计算的元素称为“ThreadGroup”

Elelement 计数:

<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
Run Code Online (Sandbox Code Playgroud)

以下 XML 中有三个 ThreadGroup 元素。我们如何使用 xmlstarlet 计算它们?

测试 XML

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="2.8" jmeter="2.13 r1665067">
<hashTree>
<TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Test Plan" enabled="true">
  <stringProp name="TestPlan.comments"></stringProp>
  <boolProp name="TestPlan.functional_mode">false</boolProp>
  <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
  <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
    <collectionProp name="Arguments.arguments"/>
  </elementProp>
  <stringProp name="TestPlan.user_define_classpath"></stringProp>
</TestPlan>
<hashTree>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
  </ThreadGroup>
  <hashTree/>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Thread Group" enabled="true">
    <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
  </ThreadGroup>
  <hashTree/>
  <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" …
Run Code Online (Sandbox Code Playgroud)

bash xmlstarlet

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

标签 统计

xmlstarlet ×10

bash ×6

xml ×5

shell ×3

sed ×1

unix ×1

xml-parsing ×1

xmllint ×1

xpath ×1

xslt ×1