【问题标题】:XML Lint parse XML [duplicate]XML Lint 解析 XML [重复]
【发布时间】:2021-02-28 20:43:07
【问题描述】:

这是我的 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="">
  <PlatformSection>
    <Kind>VMware ESXi</Kind>
    <Version>7.0.0</Version>
    <Vendor>VMware, Inc.</Vendor>
    <Locale>en</Locale>
  </PlatformSection>
  <PropertySection>
    <Property oe:key="test" oe:value="test123"/>
    <Property oe:key="testing" oe:value="testing123"/>
  </PropertySection>
  <ve:EthernetAdapterSection>
    <ve:Adapter ve:mac="" ve:network="" ve:unitNumber="7"/>
  </ve:EthernetAdapterSection>
</Environment>

我想使用 XMLLint 解析它并获取所有“oe:key”和“oe:value”

我尝试了以下命令:

  1. xmllint --xpath "string(//PlatformSection)" file1.xml

  2. xmllint --xpath "string(//Environment)" file1.xml

但两者都返回空白。同样,我尝试了 Property 和 PropertySection 并得到空白响应。

【问题讨论】:

  • 你试图做什么?什么不工作?
  • 您在默认命名空间中搜索,但您的数据位于http://schemas.dmtf.org/ovf/environment/1

标签: xml shell xmllint


【解决方案1】:

输入 XML 包含命名空间。所有元素都绑定到以下命名空间:xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"

请尝试以下 XPath:

/oe:Environment/oe:PlatformSection

或者

/oe:Environment/oe:PropertySection/oe:Property/@oe:key

或者尝试命名空间通配符方法

/*[local-name()='Environment']/*[local-name()='PlatformSection']

获取第一个oe:key属性

/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property'][1]/@oe:key

获取所有属性

/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property'][1]/@*

此处概述了另一种方法:xmllint for XML Namspace

【讨论】:

  • 我收到以下错误:XPath 错误:未定义的命名空间前缀 xmlXPathEval:评估失败 XPath 评估失败
  • 我可以获取部分,但如何获取 oe:key 和 oe:value。试过 xmllint --xpath "/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property']/@oe:key ",但得到不正确的响应
  • @kushagramittal,我将它添加到如何获取第一个 oe:key 属性的答案中。
  • xmllint --xpath "/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property '][1]" file1.xml 这可行,但现在在 xmllint --xpath "/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local -name()='属性'][1]/@oe.key" file1.xml
猜你喜欢
  • 2011-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多