【发布时间】:2013-02-07 09:32:14
【问题描述】:
我使用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']
期望的结果(可以是任何类似的东西;索引只是一个想法):
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'
我想要完成的是能够在两个这样的文件上运行 diff 或使用 grep 过滤匹配模式。我确信有一种方法可以创建这样的输出,而不使用 sed、awk 或除 xmlstarlet 本身之外的任何其他东西。
关于 xmlstarlet 和整个 xml 世界,我几乎是一个新手(不仅因为我不喜欢 xml,因为它的复杂性和解析开销等),所以我非常感谢你的帮助。谢谢!
【问题讨论】:
-
能否粘贴 XML 或其中的一部分?
-
给你:sprunge.us/RbhI(它只是一个巨大的 xml 文档的片段)
-
“我确信有一种方法可以创建这样的输出,而不使用
sed、awk或除xmlstarlet本身之外的任何其他东西。” - 除非你打算破解xmlstarlet的代码,否则我不这么认为。如果您可以假设属性值中没有空格,awk似乎是一个不错的选择。
标签: xml key-value xmlstarlet