How can I use a greater than or less than symbol with a python script embedded in xml?

StackOverflow https://stackoverflow.com/questions/20099156

  •  03-08-2022
  •  | 
  •  

Question

I am using edX Studio to make a course. I would like to make a custom python evaluated input problem. There seems to be an issue with the xml tags being closed by > or < symbols within the python code in the tag?

<?xml version="1.0"?> 
<problem>
  <p>Name as many online learning platforms as you can: </p>
    <script type="loncapa/python">

def make_a_list(name_string):
    return name_string.split(',')

def count_names(name_list):
    return len(name_list)

def how_many_oli(expect, ans):
    oli_names = ['udacity', 'udemy', 'codecademy', 'iktel'
      'codeschool', 'khan academy', 'khanacademy', 'coursera', 'edx', 'iversity']
    names = make_a_list(ans)
    how_many = len(set(names))
    message_hint = 'Good work!'
    for e in names:
        e=e.strip('"')
        e=e.strip("'")
        e=e.strip()
        e=e.lower()
        who_is = e
        if e not in oli_names:
            message_hint = message_hint+" Tell us about  "+str(who_is).title()+"?"
    if how_many < 1:
        return { 'ok': False, 'msg': 'None at all?'}
    if how_many < 5:
        return { 'ok': True, 'msg': 'Only '+str(how_many)+"?"}
    if how_many == 5:
        return { 'ok': True, 'msg': message_hint }
    if how_many > 5:
        return { 'ok': True, 'msg': message_hint }
    return False

  </script>
  <customresponse cfn="how_many_oli">
  <textline size="100" />
  </customresponse>
</problem>  

How do I avoid this? I know I could change the code to avoid using < and > but there must be a way to use them or something similar?

Was it helpful?

Solution

Text content in XML (character data) must escape the <, and & characters using pre-defined XML entities. Python code is no exception:

if how_many &lt; 1:

where < is replaced with &lt; and & with &amp;.

A proper XML parser will return text content un-escaped, replacing such entities with the original characters.

OTHER TIPS

< and > are XML Entities. You'll need to escape them i.e. you'll need to use &lt; and &gt; instead. And if you use & itself, &amp;.

If this is a pain, you can also put the whole thing in a CDATA section:

http://www.w3schools.com/xml/xml_cdata.asp

looks like this:

<script>
<![CDATA[
if how_many < 1:
    return { 'ok': False, 'msg': 'None at all?'}
]]>
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top