1

I need some help. I am trying to display the value in a tabular format but getting the following error using Django and Python. The error is given below.

raise SyntaxError("invalid predicate")

SyntaxError: invalid predicate

def search(request):
    per=[]
    if request.method == 'POST':
        rname=request.POST.get('rname')
        serch=request.POST.get('searchby')
        tree = ET.parse('roomlist.xml')
        root = tree.getroot()
        if serch==0:
           xyz = './/*[roomname='
           xyz = xyz + rname
           xyz= xyz + ']'
           result=root.findall(xyz)
           for x in result:
                  per.append({'roomname':x.find('roomname').text,'seat':x.find('noseats').text,'project':x.find('projectorscreen').text,'video':x.find('videoconf').text})
        return render(request,'booking/home.html',{'people': per}) 

My view part is given below.

home.html:

{% if people %}
        <table>
            <tr>
                <th>Room Name</th>
                <th>seats</th>
                <th>Projector screen</th>
                <th>Video Conference</th>
            </tr>
        {% for person in people %}
            <tr>
                <td>{{person.roomname}}</td>
                <td>{{person.seat}}</td>
                <td>{{person.project}}</td>
                <td>{{person.video}}</td>
            </tr>
        {% endfor %}
        </table>
    {% else %}
        <p>No Record in the database</p>
    {% endif %}

I need to fetch all value from the below .xml file.

<?xml version="1.0" ?><roomlist>
  <location name="Bangalore">
    <room id="1uy92j908u092">
      <roomname> Aquarius </roomname>
      <noseats> 10 </noseats>
      <projectorscreen>yes</projectorscreen>
      <videoconf>yes</videoconf>
    </room>
  </location>
<location name="Bhubaneswar"><room id="131198912460"><roomname>cottage</roomname><noseats>5</noseats><projectorscreen>Yes</projectorscreen><videoconf>Yes</videoconf></room></location><location name="puri"><room id="509955554930"><roomname>room1</roomname><noseats>10</noseats><projectorscreen>No</projectorscreen><videoconf>Yes</videoconf></room></location></roomlist>

Here I need to search all value from file and display. Please help me.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895

1 Answers1

0

It looks like you are using xml module to parse xmls. Unfortunatelly findall of xml module only supports subset of xpath.

So my suggestion would be to use lxml instead:

from lxml import  etree as ET 

and then:

if serch==0:
    xyz = './/room/roomname[text()="{}"]'.format(rname)
    result=root.xpath(xyz)
running.t
  • 5,329
  • 3
  • 32
  • 50
  • suppose I have `rname='cottage/'` then in this case how to escape the special string. –  Jun 16 '17 at 11:37
  • [Here](https://stackoverflow.com/questions/18935754/how-to-escape-special-characters-of-a-string-with-single-backslashes) you can find some help on escaping characters. – running.t Jun 16 '17 at 11:44