1

This is my XML file

<?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>

I want to parse it using XMLLint and fetch all "oe:key" and "oe:value"

I tried following commands:

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

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

But both are returning blank. Similarly, I tried for Property and PropertySection and getting a blank response.

kushagra mittal
  • 343
  • 5
  • 17

1 Answers1

1

The input XML contains namespaces. All elements are bound to the following namespace: xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"

Please try the following XPath:

/oe:Environment/oe:PlatformSection

Or

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

Or try namespace wildcard approach

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

To get first oe:key attribute

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

To get all attributes

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

One more approach is outlined here: xmllint for XML Namspace

Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • I am getting below error: XPath error : Undefined namespace prefix xmlXPathEval: evaluation failed XPath evaluation failure – kushagra mittal Nov 17 '20 at 16:39
  • I am able to fetch Section but how to fetch oe:key and oe:value. Tried xmllint --xpath "/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property']/@oe:key", but getting incorrect response – kushagra mittal Nov 18 '20 at 13:24
  • @kushagramittal, I added it to the answer how to get the first **oe:key** attribute. – Yitzhak Khabinsky Nov 18 '20 at 13:53
  • xmllint --xpath "/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property'][1]" file1.xml This works but now one below xmllint --xpath "/*[local-name()='Environment']/*[local-name()='PropertySection']/*[local-name()='Property'][1]/@oe.key" file1.xml – kushagra mittal Nov 18 '20 at 14:16