lon*_*ckz 14 python xml lxml gpx
我正在尝试使用Pythons LXML库创建一个可由Garmin的Mapsource产品读取的GPX文件.它们的GPX文件上的标题如下所示
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"
creator="MapSource 6.15.5" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码时:
xmlns = "http://www.topografix.com/GPX/1/1"
xsi = "http://www.w3.org/2001/XMLSchema-instance"
schemaLocation = "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
version = "1.1"
ns = "{xsi}"
getXML = etree.Element("{" + xmlns + "}gpx", version=version, attrib={"{xsi}schemaLocation": schemaLocation}, creator='My Product', nsmap={'xsi': xsi, None: xmlns})
print(etree.tostring(getXML, xml_declaration=True, standalone='Yes', encoding="UTF-8", pretty_print=True))
Run Code Online (Sandbox Code Playgroud)
我明白了:
<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\'?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1" xmlns:ns0="xsi"
ns0:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
version="1.1" creator="My Product"/>
Run Code Online (Sandbox Code Playgroud)
哪个有烦人的ns0标签.这可能是完全有效的XML,但Mapsource并不欣赏它.
知道怎么让这个没有ns0标签吗?
use*_*019 13
问题出在您的属性名称上.
attrib={"{xsi}schemaLocation" : schemaLocation},
Run Code Online (Sandbox Code Playgroud)
将schemaLocation放在xsi名称空间中.
我想你的意思
attrib={"{" + xsi + "}schemaLocation" : schemaLocation}
Run Code Online (Sandbox Code Playgroud)
使用xsi的URL.这与您在元素名称中使用命名空间变量相匹配.它将属性放在http://www.w3.org/2001/XMLSchema-instance命名空间中
这给出了结果
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
version="1.1"
creator="My Product"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5631 次 |
| 最近记录: |