我正在使用edX Studio制作课程。我想做一个自定义的python评估输入问题。似乎有一个问题,xml标签被标签中的python代码中的>
或<
符号关闭?
<?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>
我该如何避免这种情况?我知道我可以更改代码以避免使用<
和>
,但必须有办法使用它们或类似的东西吗?
答案 0 :(得分:7)
XML中的文字内容(character data)必须使用pre-defined XML entities转义<
和&
个字符。 Python代码也不例外:
if how_many < 1:
其中<
替换为<
,&
替换为&
。
正确的XML解析器将返回未转义的文本内容,将这些实体替换为原始字符。
答案 1 :(得分:4)
<
和>
是XML实体。您需要转义它们,即您需要使用<
和>
。如果您使用&
本身,&
。
如果这很痛苦,你也可以把整件事放在CDATA部分:
http://www.w3schools.com/xml/xml_cdata.asp
看起来像这样:
<script>
<![CDATA[
if how_many < 1:
return { 'ok': False, 'msg': 'None at all?'}
]]>
</script>