1
votes

With lots of help from stack overflow, I managed to get some python code working to process xml files (using lxml). I've been able to adapt it for lots of different purposes, but there is one thing I can't work out.

Example XML:

<?xml version="1.0" encoding="UTF-8" ?>
  <TVAMain xml:lang="PL" publisher="Someone" publicationTime="2014-01-03T06:24:24+00:00" version="217"   xmlns="urn:tva:metadata:2010" xmlns:mpeg7="urn:tva:mpeg7:2008"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:tva:metadata:2010 http://Something.xsd">
    <ProgramDescription>
      <ProgramInformationTable>
        <ProgramInformation programId="crid://bds.tv/88032"><BasicDescription>
          <Title xml:lang="PL" type="episodeTitle">Some Title</Title>
            <Synopsis xml:lang="PL" length="short">Some Synopsis</Synopsis>
              <Genre  href="urn:tva:metadata:cs:EventGenreCS:2009:96">
                <Name xml:lang="EN">Some Genre</Name>
              </Genre>
              <Language>PL</Language>
              <RelatedMaterial>
                <HowRelated href="urn:eventis:metadata:cs:HowRelatedCS:2010:boxCover">
                <Name>Box cover</Name>
                </HowRelated>
              <MediaLocator>
                <mpeg7:MediaUri>file://Images/98528834.p.jpg</mpeg7:MediaUri>
              </MediaLocator>
            </RelatedMaterial>

The python code will return the Title, Genre and Synopsis, but it will not return the image reference (3rd line from the bottom). I presume this is because of the name format 'mpeg7:MediaUri' (which I cannot change). The code will return the 'No Image' string instead.

This is the relavent python code

file_name = input('Enter the file name, including .xml extension: ')
print('Parsing ' + file_name)

from lxml import etree

parser = etree.XMLParser()

tree = etree.parse(file_name, parser)
root = tree.getroot()

nsmap = {'xmlns': 'urn:tva:metadata:2010'} 

with open(file_name+'.log', 'w', encoding='utf-8') as f:

   for info in root.xpath('//xmlns:ProgramInformation', namespaces=nsmap):

   crid = (info.get('programId'))

   titlex = (info.find('.//xmlns:Title', namespaces=nsmap))           
   title = (titlex.text if titlex != None else 'No title')  

   genrex = (info.find('.//xmlns:Genre/xmlns:Name', namespaces=nsmap))
   genre = (genrex.text if genrex != None else 'No Genre')

   imagex = (info.find('.//xmlns:RelatedMaterial/xmlns:MediaLocator/xmlns:"mpeg7:MediaUri"', namespaces=nsmap))
   image = (image.text if imagex != None else 'No Image')

   f.write('{}|{}|{}|{}\n'.format(crid, title, genre, image))

Can someone explain how I can adapt the 'imagex' line, so that it returns 'file://Images/98528834.p.jpg' from the example? I had a look at using square brackets, but it caused an error.

1

1 Answers

1
votes

That node you are interested in, has mpeg7 namespace instead of default namespace. You can try with this syntax *[local-name() = "elementName"] to match element by it's local name (ignoring the namespace) :

imagex = info.xpath(
    './/xmlns:RelatedMaterial/xmlns:MediaLocator/*[local-name() = "MediaUri"]',
   namespaces=nsmap)[0]

Or add the mpeg7 in namespaces declaration :

nsmap = {'xmlns': 'urn:tva:metadata:2010', 'mpeg7':'urn:tva:mpeg7:2008'} 

then you can use mpeg7 prefix in xpath query :

imagex = (info.find('.//xmlns:RelatedMaterial/xmlns:MediaLocator/mpeg7:MediaUri', namespaces=nsmap))