标签: xmlstarlet

XML Starlet的替代品

有没有替代XML starletXML处理bash?我的典型用法是:

  • 相当格式化;
  • 选择数据XPath.

xml bash xmlstarlet

6
推荐指数
1
解决办法
2244
查看次数

将xml转换为键值对符号

xmlstarlet el -v用来显示xml文件的结构,包括所有属性和值.我想将其输出转换为某种键值对,即每个属性的值在一个单独的行上(包括XPath); 每一行必须是唯一的.

目前的结果:

topRoot/topSystem/commSvcEp/commSyslog[@descr='Syslog Service' and @name='syslog' and @policyOwner='local' and @severity='critical']
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[@adminState='disabled' and @forwardingFacility='local7' and @hostname='none' and @name='secondary' and @severity='critical']
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[@adminState='disabled' and @forwardingFacility='local7' and @hostname='none' and @name='tertiary' and @severity='critical']
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[@adminState='disabled' and @forwardingFacility='local7' and @hostname='none' and @name='primary' and @severity='critical']
Run Code Online (Sandbox Code Playgroud)

期望的结果(可能是类似的;指数只是一个想法):

topRoot/topSystem/commSvcEp/commSyslog@descr='Syslog Service'
topRoot/topSystem/commSvcEp/commSyslog@name='syslog'
topRoot/topSystem/commSvcEp/commSyslog@policyOwner='local'
topRoot/topSystem/commSvcEp/commSyslog@severity='critical'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[0]@adminState='disabled'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[0]@forwardingFacility='local7'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[0]@hostname='none'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[0]@name='secondary'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[0]@severity='critical'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[1]@adminState='disabled'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[1]@forwardingFacility='local7'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[1]@hostname='none'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[1]@name='tertiary'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[1]@severity='critical'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[2]@adminState='disabled'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[2]@forwardingFacility='local7'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[2]@hostname='none'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[2]@name='primary'
topRoot/topSystem/commSvcEp/commSyslog/commSyslogClient[2]@severity='critical'
Run Code Online (Sandbox Code Playgroud)

我想要完成的是能够运行diff两个这样的文件或使用grep过滤匹配模式.我确信有一种方法可以创建这样的输出,不使用sed,awk或者除了xmlstarlet它自己以外的任何其他东西.

我几乎是关于xmlstarlet和整个xml世界的新手(不仅因为它因为它的复杂性和解析开销等而不喜欢xml),所以我非常感谢你的帮助.谢谢!

xml key-value xmlstarlet

6
推荐指数
1
解决办法
4435
查看次数

xmlstarlet - 删除换行符

我有以下文件log.xml:

<entry>
  <message>Line 1
  Line 2 and so on</message>
</entry>
Run Code Online (Sandbox Code Playgroud)

在CentOS 5.4上使用xmlstarlet 1.0.1,如果我运行以下命令,同时删除换行符message:

xml sel -t -m //entry -v "translate(message,'&#xa;' ,'@')" log.xml
Run Code Online (Sandbox Code Playgroud)

结果是:

Line 1@  Line 2 and so on
Run Code Online (Sandbox Code Playgroud)

在CentOS 6上使用xmlstarlet:

1.3.1
compiled against libxml2 2.7.6, linked with 20706
compiled against libxslt 1.1.26, linked with 10126
Run Code Online (Sandbox Code Playgroud)

我会收到:

Line 1
  Line 2 nd so on
Run Code Online (Sandbox Code Playgroud)

注意"和"转换为空.我想这不是xmlstarlet的问题,而是libxslt的一些变化.

任何想法如何解决它?

UPDATE

添加字母"a"转换为空的问题.

xml linux xmlstarlet

6
推荐指数
1
解决办法
1849
查看次数

根据树中另一个元素的值更改XML元素的值

我有一个非常大的(100+兆字节未压缩)XML文件存储数据集,我正在尝试有选择地更改一些值.

例如,说sample.xml看起来像这样:

<objects>
  <object>
    <name>Foo</name>
    <constant1>10</constant1>
    <constant2>20</constant2>
  </object>
  <object>
    <name>Bar</name>
    <constant1>15</constant1>
    <constant2>40</constant2>
  </object>
<objects>
Run Code Online (Sandbox Code Playgroud)

现在我想将值更改<constant1>为18,但适用于其Name元素具有值的对象Foo.我一直在讨论XML Starlet文档,但它关于编辑的文章只有关于如何直接在树中查找元素属性的示例,除非我遗漏了一些东西......

xml xmlstarlet

6
推荐指数
1
解决办法
5194
查看次数

使用 xmlstarlet 进行 XPath 查询

我有与此类似的 XML:

<orders>
        <orderCompleteRequestType>
                <Head>
                        <Aufnr>11111</Aufnr>
                </Head>
                <Register>
                        <Id>180</Id>
                        <value1>11</value1>
                        <value2>22</value2>
                </Register>
                <Register>
                        <Id>181</Id>
                        <value1>3</value1>
                        <value2>43</value2>
                </Register>
                <Register>
                        <Id>160</Id>
                        <value1>5</value1>
                        <value2>25</value2>
                </Register>
        </orderCompleteRequestType>
        <orderCompleteRequestType>
                <Head>
                        <Aufnr>22222</Aufnr>
                </Head>
                <Register>
                        <Id>280</Id>
                        <value1>1</value1>
                        <value2>12</value2>
                </Register>
                <Register>
                        <Id>160</Id>
                        <value1>12</value1>
                        <value2>7</value2>
                </Register>
                <Register>
                        <Id>281</Id>
                        <value1>94</value1>
                        <value2>22</value2>
                </Register>
        </orderCompleteRequestType>
</orders>
Run Code Online (Sandbox Code Playgroud)

我想以 CSV 格式从每个“orderCompleteRequestType”结构中选择一些值:

  • 头/奥夫恩
  • 注册/ID
  • 寄存器/值1
  • 寄存器/值2

使用以下命令行时:

xmlstarlet sel -T -t -m "/orders/orderCompleteRequestType" -v "Head/Aufnr" -o ";" -v "Register/Id" -o ";" -v "Register/value1" -o ";" -v "Register/value2" -n -n test.xml
Run Code Online (Sandbox Code Playgroud)

我得到:

11111;180
181
160;11
3 …
Run Code Online (Sandbox Code Playgroud)

xml xpath xmlstarlet

6
推荐指数
1
解决办法
3817
查看次数

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 中的多个属性?

例如,给定:

<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
查看次数

如何使用 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 或 xmllint 获取属性值

自过去两天以来,我已经解决了几个问题,但尚未找到解决方案。这是我的 xml:

<?xml version="1.0" encoding="UTF-8"?>

<Environment xmlns="http://schemas.dmtf.org/ovf/environment/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oe="http://schemas.dmtf.org/ovf/environment/1" xmlns:ve="http://www.vmware.com/schema/ovfenv" oe:id="" ve:vCenterId="">
  <PropertySection>
<Property oe:key="vami.hostname" oe:value="jal"/>
<Property oe:key="vamitimezone" oe:value="Asia/Kolkata"/>
<Property oe:key="ABC_enable" oe:value="1"/>
<Property oe:key="software_only_installer_name" oe:value="install-r8-0-0-0"/>
<Property oe:key="software_only_staging_dir" oe:value="/media/dir"/>
<Property oe:key="software_only_mount_dir" oe:value="/media/cdrom"/>
  </PropertySection>
</Environment>
Run Code Online (Sandbox Code Playgroud)

我想在 oe:key="ABC_enable" 时获取属性值(oe:value)。

我用 xmllint 和 xmlstarlet 尝试了很多次,但没有得到我想要的。你能帮忙吗?

xml awk sed xmlstarlet xmllint

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

标签 统计

xmlstarlet ×10

xml ×8

bash ×2

xpath ×2

awk ×1

key-value ×1

linux ×1

sed ×1

xmllint ×1

xslt ×1