這是我在這裏的第一篇文章...如何更改Hy中XML標籤的名稱?
我想在Hy(運行於Python 3)中使用assignment更改XML元素。
當我嘗試運行代碼片段:
(setv tco (BeautifulSoup "<meek/>" 'xml))
(setv (. tco meek name) "defl")
(print (. tco meek name))
;(assoc (. tco defl) "new" "value")
(.append (. tco meek) "Kool Stuff")
(print (.prettify tco))
我得到的錯誤:
Traceback (most recent call last): File "/usr/bin/hy", line 9, in load_entry_point('hy==0.11.0', 'console_scripts', 'hy')() File "/usr/lib/python3/dist-packages/hy/cmdline.py", line 347, in hy_main sys.exit(cmdline_handler("hy", sys.argv)) File "/usr/lib/python3/dist-packages/hy/cmdline.py", line 335, in cmdline_handler return run_file(options.args[0]) File "/usr/lib/python3/dist-packages/hy/cmdline.py", line 210, in run_file import_file_to_module("main", filename) File "/usr/lib/python3/dist-packages/hy/importer.py", line 78, in import_file_to_module eval(ast_compile(_ast, fpath, "exec"), mod.dict) File "code-trace.hy", line 196, in (setv (. tco meek name) "defl") AttributeError: 'NoneType' object has no attribute 'name'
與第二行中運行評論說:
(setv tco (BeautifulSoup "<meek/>" 'xml))
;(setv (. tco meek name) "defl")
(print (. tco meek name))
;(assoc (. tco defl) "new" "value")
(.append (. tco meek) "Kool Stuff")
(print (.prettify tco))
沒有給出錯誤(必須使用代碼而不是blockquote標記,以便正確顯示):
meek
<?xml version="1.0" encoding="utf-8"?>
<meek>
Kool Stuff
</meek>
請注意,這只是一個較大程序的一些測試代碼。我在第四行的註釋assoc也有問題。