從這個字串:
label_config={
"label1": [
"modality1",
"modality2",
"modality3"],
"choice":"single",
"required": "true",
"name" : "sentiment"},{
"label2": [
"modality1",
"modality2"],
"name" : "price"
}
我創建了這個列印的 XML:

任何人都知道如何感謝這個庫: from lxml import etree 可以將黃色元素的斜線從末尾移動到開頭?
這是一代的代碼:
from lxml import etree
import sys
def topXML(dictAttrib = None):
root : {lxml.etree._Element}
root = etree.Element("View")
textEl = etree.SubElement(root, "Text")
if dictAttrib == None:
dictAttrib = {
"name":"text",
"value":"$text"
}
for k_,v_ in dictAttrib.items():
textEl.set(k_,v_)
return root
def choiceXML(root,locChoice):
headerEl = etree.SubElement(root, "Header")
choisesEl = etree.SubElement(root, "Choices")
for k_,v_ in locChoice.items():
if (isinstance(k_,str) & isinstance(v_,list)):
choices = v_
headerEl.set("value",k_)
if locChoice.get("toName") == None:
choisesEl.set("toName","text")
for op_ in choices:
opEl = etree.SubElement(root, "Choice")
opEl.set("value",op_)
else :
choisesEl.set(k_,v_)
choisesEl = etree.SubElement(root, "Choices")
return root
def checkConfig(locChoice):
if locChoice.get("name") == None :
sys.exit("Warning : label_config needs a parameter called 'name' assigned")
def xmlConstructor(label_config):
root = topXML()
for ch_ in label_config:
checkConfig(ch_)
root = choiceXML(root,ch_)
return root
生成的代碼將在此站點https://labelstud.io/playground/中使用。他們使用某種型別的 XML 來創建代碼。不幸的是,使用 etree 并沒有實作想要的產品,我發現如果我進行了上述更改,它將起作用。
與此同時,我正在聯系他們的團隊以獲取更多資訊,但如果這里的某人對如何使其作業有任何想法,請挺身而出。
我對所有建議持開放態度,但是我正在尋找結構良好的解決方案,而不是通過附加代碼來創建代碼。
uj5u.com熱心網友回復:
要正確封裝<Choice>其父節點下的節點<Choices>,只需對您的方法進行以下非常簡單的兩個更改choiceXML。即,在元素(不是)opEl下添加子元素,并在末尾洗掉多余的第二行。choisesElrootchoisesEl
def choiceXML(root, locChoice):
headerEl = etree.SubElement(root, "Header")
choisesEl = etree.SubElement(root, "Choices")
for k_,v_ in locChoice.items():
if (isinstance(k_,str) & isinstance(v_,list)):
choices = v_
headerEl.set("value",k_)
if locChoice.get("toName") == None:
choisesEl.set("toName","text")
for op_ in choices:
opEl = etree.SubElement(choisesEl, "Choice") # CHANGE root to choisesEl
opEl.set("value",op_)
else :
choisesEl.set(k_,v_)
#choisesEl = etree.SubElement(root, "Choices") # REMOVE THIS LINE
return root
全流程
label_config = {
"label1": [
"modality1",
"modality2",
"modality3"],
"choice":"single",
"required": "true",
"name" : "sentiment"},{
"label2": [
"modality1",
"modality2"],
"name" : "price"
}
def topXML(dictAttrib = None):
# ...NO CHANGE...
def choiceXML(root,locChoice):
# ...ABOVE CHANGE...
def checkConfig(locChoice):
# ...NO CHANGE...
def xmlConstructor(label_config):
# ...NO CHANGE...
output = xmlConstructor(label_config)
輸出
print(etree.tostring(output, pretty_print=True).decode("utf-8"))
# <View>
# <Text name="text" value="$text"/>
# <Header value="label1"/>
# <Choices toName="text" choice="single" required="true" name="sentiment">
# <Choice value="modality1"/>
# <Choice value="modality2"/>
# <Choice value="modality3"/>
# </Choices>
# <Header value="label2"/>
# <Choices toName="text" name="price">
# <Choice value="modality1"/>
# <Choice value="modality2"/>
# </Choices>
# </View>
uj5u.com熱心網友回復:
The<Choices/>是<Choices></Choices>(XML 規范)的縮寫。如果你只是把它作為一個結束元素,你可能沒有一個開始元素,結果將是無效的 xml。任何試圖讀取/決議的程式都會出錯。
請注意,所有元素上都有斜杠<Choices>,非空元素也是如此。
如果您不想要空<Choices/>元素,則可能需要研究如何從 dict 生成 XML。由于您不提供MCVE,我們無法回答該部分。
uj5u.com熱心網友回復:
這更像是一個評論而不是一個答案,但評論有點太長了。查看您提供的內容,似乎問題不在于您的 xml 格式太好(沒有這樣的東西),或者操場上有某種奇怪的 xml 結構。我相信您生成的 xml 不是他們想要的。
如果您查看第二個<Choices>元素,則顯示為
<Choices toName="text" name="price"/>
嘗試洗掉關閉/,使其顯示為:
<Choices toName="text" name="price">
然后它將通過以下方式關閉,<Choices/>也許它會起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474955.html
上一篇:XSL只顯示第一個值
