5
當我使用minidom創建文檔時,屬性在元素中按字母順序排序。從here拿這個例子:XML屬性得到排序
from xml.dom import minidom
# New document
xml = minidom.Document()
# Creates user element
userElem = xml.createElement("user")
# Set attributes to user element
userElem.setAttribute("name", "Sergio Oliveira")
userElem.setAttribute("nickname", "seocam")
userElem.setAttribute("email", "[email protected]")
userElem.setAttribute("photo","seocam.png")
# Append user element in xml document
xml.appendChild(userElem)
# Print the xml code
print xml.toprettyxml()
結果是這樣的:
<?xml version="1.0" ?>
<user email="[email protected]" name="Sergio Oliveira" nickname="seocam" photo="seocam.png"/>
這都非常好,如果你想在電子郵件/名/暱稱/照片順序,而不是名稱/暱稱的屬性/電子郵件/照片訂單,因爲它們被創建。
如何獲得按照創建順序顯示的屬性?或者,你如何控制訂單?
你爲什麼要這樣? XML中的屬性根據定義沒有順序(與元素相反)。這只是爲了美化嗎? – Boldewyn 2009-11-16 08:03:47
如果我有一個XSD按順序指定C-B-A的屬性,並且我只能按照A-B-C的順序對它們進行描述,我是否滿足XSD? – hughdbrown 2009-11-16 13:39:02