0

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?

nicholaschris
  • 1,401
  • 20
  • 27

2 Answers2

7

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
4

< 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>
Corley Brigman
  • 11,633
  • 5
  • 33
  • 40
  • @KDawG Sure, this happens a lot on SO : people often post their answer at the same time... –  Nov 20 '13 at 15:03
  • @arbautjc therefore since he posted a minute late *a same answer* he'd consider removing.... – K DawG Nov 20 '13 at 15:06
  • 1
    @KDawG: So? That doesn't mean someone else cannot try and explain it in a different way. That's what SO is all about, competing for votes by posting better answers. – Martijn Pieters Nov 20 '13 at 15:06
  • @MartijnPieters yeah guess your right, he mentioned a `xml_cdata` section which is seems to be more useful than using `< and >` *if only he could provide a example..* – K DawG Nov 20 '13 at 15:12
  • good point. there is a better example on the web page, but added a short example. was going to remove, but the cdata thing was different, and honestly easier (have had to do this myself)... – Corley Brigman Nov 20 '13 at 15:22
  • Are either of these considered better practice? ie. Using `<` or use `<![CDATA[ ...code... ]]>`? – nicholaschris Nov 20 '13 at 15:27
  • 1
    i don't think it matters, but if you use CDATA, then you can copy & paste your code into a console for debugging. you can still do this with entities, but you have to load the file first as xml (to get back the original string), echo to the screen, etc. i am not an XML expert, but in generally they provide both global and targeted ways to do things (see e.g. namespaces for similar examples). – Corley Brigman Nov 20 '13 at 15:34