Finden Sie den XSD-zu-CSV-Parser wie unten angegeben: Mit dem unten stehenden Code können auch mehrere Knoten-XMLs geparst werden.
import pandas as pd
from bs4 import BeautifulSoup
def xsd_to_dict(xsd_path):
super_dict = {}
soup = BeautifulSoup(open(xsd_path), "html.parser")
for complex_type in soup.find_all('xs:complextype'):
xsd_parsed = [x for x in ",".join(str(complex_type).split("\n"))
.replace("", "")
.replace("'", "")
.replace("", "")
.replace("", "")
.replace(">", "").replace("sequence", "")
.split(",") if x != ""]
if len(xsd_parsed[0]) > len("complextype") + 1:
matrix_list = [e.split(" ") for e in xsd_parsed[-len(xsd_parsed) + 1:]]
level_1 = ["|".join(["".join([":".join(final.split("=")) for final in y if len(final.split("=")) == 2])
for y in [x.split(",") for x in item]]) for item in matrix_list]
level_1.insert(0, xsd_parsed[0])
for x in level_1[-len(xsd_parsed) + 1:]:
flattened_dict = {x.split(":")[0]:"-".join(x.split(":")[-len(x.split(":")) + 1:])
for x in (level_1[0] + x).replace("=", ":").split("|")}
xPath = flattened_dict.get("complextype name")
xmlName = flattened_dict.get("name")
dataType = flattened_dict.get("type")
if xmlName != None:
final_dict = {x.split(":")[0]:x.split(":")[1]
for x in str("xpath:"+str(xPath)+",xmlFieldName:"+str(xmlName)+",dataPath:"+str(dataType)).split(",")}
for k, v in final_dict.items():
super_dict.setdefault(k, []).append(v)
return super_dict
def xsd_to_csv(xsd_path):
pd.DataFrame(xsd_to_dict(xsd_path)).to_csv(xsd_path.replace(".xsd", ".csv"))
return "done"
xsd_to_csv("CustomersOrders.xsd")
Eingabe: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/sample-xsd-file-customers-and-orders1
Ausgabe:
,xpath,xmlFieldName,dataPath
0,"""CustomerType""","""CompanyName""","""xs-string"""
1,"""CustomerType""","""ContactName""","""xs-string"""
2,"""CustomerType""","""ContactTitle""","""xs-string"""
3,"""CustomerType""","""Phone""","""xs-string"""
4,"""CustomerType""","""Fax""","""xs-string"""
5,"""CustomerType""","""FullAddress""","""AddressType"""
6,"""CustomerType""","""CustomerID""","""xs-token""