有没有替代XML starlet
XML处理bash
?我的典型用法是:
XPath
.我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),所以我非常感谢你的帮助.谢谢!
我有以下文件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,'
' ,'@')" 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"转换为空的问题.
我有一个非常大的(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:
<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”结构中选择一些值:
使用以下命令行时:
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) 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) 我正在尝试使用 xmlstarlet 格式化 xml 文件,但我不想创建新的 xml 文件。
我试过这个
xmlstarlet fo --inplace --indent-tab --omit-decl project_00.xml
Run Code Online (Sandbox Code Playgroud)
--inplace
但 (format) 命令不允许使用该参数fo
。
有谁知道我该怎么做?
例如,给定:
<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) 我想在之后添加这个块</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) 自过去两天以来,我已经解决了几个问题,但尚未找到解决方案。这是我的 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 尝试了很多次,但没有得到我想要的。你能帮忙吗?