15

Python の 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">

次のコードを使用すると:

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))

私は得る:

<?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"/>

迷惑なns0タグが付いています。これは完全に有効な XML かもしれませんが、Mapsource はそれを高く評価していません。

ns0これにタグを付けないようにする方法はありますか?

4

1 に答える 1

16

問題は属性名にあります。

attrib={"{xsi}schemaLocation" : schemaLocation},

schemaLocation を xsi 名前空間に配置します。

私はあなたが意味したと思います

attrib={"{" + xsi + "}schemaLocation" : schemaLocation}

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"/>
于 2010-05-17T16:32:17.673 に答える