使用xmlstarlet或xmllint获取属性值 [英] get attribute value using xmlstarlet or xmllint

查看:376
本文介绍了使用xmlstarlet或xmllint获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have gone through several of questions since last two days but yet to find a solution. Here is my 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>

当oe:key ="ABC_enable"时,我想获取属性值(oe:value).

I want to get attribute value(oe:value) when oe:key="ABC_enable".

我已经使用xmllint和xmlstarlet尝试了很多次,但无法获得我想要的东西. 你能帮忙吗?

I have tried many times with xmllint and xmlstarlet but couldn't get what I want. Can you please help?

推荐答案

使用 xmlstarlet 工具的正确方法:

The right way with xmlstarlet tool:

xmlstarlet sel -N oe="http://schemas.dmtf.org/ovf/environment/1" \
-N ve="http://www.vmware.com/schema/ovfenv" --net -t -v \
'//oe:Property[@oe:key="ABC_enable"]/@oe:value' -n input.xml

输出:

1

这篇关于使用xmlstarlet或xmllint获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆