Question

The file contains the following lines.

<?xml version="1.0" encoding="UTF-8"?>
<FVDL xmlns="xmlns://www.fortifysoftware.com/schema/fvdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.9" xsi:type="FVDL">`
<CreatedTS date="2013-08-06" time="11:8:48" />`

I am trying to read the version tag in FVDL.I am using lxml etree and my code snippet is

from lxml import etree
with open(os.path.join(analysis,"merged-results.fvdl") ,"r") as file_handle:
  context = etree.parse(file_handle)
  ver = context.xpath('//FVDL')
  print ver

This had worked before in parsing a standard xml file. However it is failing for the above mentioned file .(ver is an empty list at the end of execution)

Was it helpful?

Solution

Alternative to @falsetru's answer

(By "trying to read the version tag", I understand "the version attribute" (which may not be what you want))

Explicitly register fvdl namespace, under the "fvdl" prefix:

ver = context.xpath('//fvdl:FVDL/@version',
          namespaces={"fvdl": "xmlns://www.fortifysoftware.com/schema/fvdl"})

Or, riskier, if somehow you know you want the version attribute from the root node

ver = context.xpath('/*/@version')

Both give ['1.9']

OTHER TIPS

context = etree.parse(file_handle)
ver = context.getroot()
print ver.attrib['version']

output:'1.9'

Use [local-name()=...]:

ver = context.xpath('//*[local-name()="FVDL"]')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top