首頁 >後端開發 >Python教學 >無法使用架構驗證 XML,但可以透過從中讀取寫入的檔案來運作

無法使用架構驗證 XML,但可以透過從中讀取寫入的檔案來運作

WBOY
WBOY轉載
2024-02-22 12:34:02712瀏覽

无法使用架构验证 XML,但可以通过从中读取写入的文件来工作

問題內容

我目前正在使用 lxml 並希望驗證 xml 內容。

我從tei = etree.element("tei", nsmap={none: 'http://www.tei-c.org/ns/1.0'} 完全用python 寫,包含許多子元素。 p>

現在,我想使用以下程式碼使用特定的 .xsd 檔案檢查結構是否正確:

xmlschema_doc = etree.parse(xsd_file_path)
xmlschema = etree.xmlschema(xmlschema_doc)
# run check
status = xmlschema.validate(xml_tree)

它傳回 false,並顯示錯誤 element 'tei':沒有可用於驗證 root. 的匹配全域聲明

我觀察到一件非常奇怪的事情,如果我使用 寫 xml

et = etree.elementtree(xmldata)
et.write('test.xml', pretty_print=true, xml_declaration=true, encoding='utf-8')

如果我用b= etree.parse('test.xml') 重新打開它,我最終沒有錯誤,並且由於xmlschema.validate(b) 的結果, xml 結構是有效的

知道我需要在 xml 結構中添加什麼嗎?

編輯: 無效 xml 中的第一項

有效 xml 檔案中的第一項

編輯:

<?xml version='1.0' encoding='UTF-8'?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
  <text>
    <body>
      <listBibl>
        <biblFull>
          <titleStmt>
            <title xml:lang="en">article</title>
            <title xml:lang="fr">article</title>
            <title type="sub" xml:lang="en">A subtitle</title>
            <author role="aut">
              <persName>
                <forename type="first">John</forename>
                <surname>Doe</surname>
              </persName>
              <email>email</email>
              <idno type="http://orcid.org/">orcid</idno>
              <affiliation ref="#localStruct-affiliation"/>
              <affiliation ref="#struct-affiliation"/>
            </author>
            <author role="aut">
              <persName>
                <forename type="first">Jane</forename>
                <forename type="middle">Middle</forename>
                <surname>Doe</surname>
              </persName>
              <email>email</email>
              <idno type="http://orcid.org/">orcid</idno>
              <affiliation ref="#localStruct-affiliationA"/>
              <affiliation ref="#localStruct-affiliationB"/>
            </author>
          </titleStmt>
          <editionStmt>
            <edition>
              <ref type="file" subtype="author" n="1" target="upload.pdf"/>
            </edition>
          </editionStmt>
          <publicationStmt>
            <availability>
              <licence target="https://creativecommons.org/licenses//cc-by/"/>
            </availability>
          </publicationStmt>
          <notesStmt>
            <note type="audience" n="2"/>
            <note type="invited" n="1"/>
            <note type="popular" n="0"/>
            <note type="peer" n="1"/>
            <note type="proceedings" n="0"/>
            <note type="commentary">small comment</note>
            <note type="description">small description</note>
          </notesStmt>
          <sourceDesc>
            <biblStruct>
              <analytic>
                <title xml:lang="en">article</title>
                <title xml:lang="fr">article</title>
                <title type="sub" xml:lang="en">A subtitle</title>
                <author role="aut">
                  <persName>
                    <forename type="first">John</forename>
                    <surname>Doe</surname>
                  </persName>
                  <email>email</email>
                  <idno type="http://orcid.org/">orcid</idno>
                  <affiliation ref="#localStruct-affiliation"/>
                  <affiliation ref="#struct-affiliation"/>
                </author>
                <author role="aut">
                  <persName>
                    <forename type="first">Jane</forename>
                    <forename type="middle">Middle</forename>
                    <surname>Doe</surname>
                  </persName>
                  <email>email</email>
                  <idno type="http://orcid.org/">orcid</idno>
                  <affiliation ref="#localStruct-affiliationA"/>
                  <affiliation ref="#localStruct-affiliationB"/>
                </author>
              </analytic>
              <monogr>
                <idno type="isbn">978-1725183483</idno>
                <idno type="halJournalId">117751</idno>
                <idno type="issn">xxx</idno>
                <imprint>
                  <publisher>springer</publisher>
                  <biblScope unit="serie">a special collection</biblScope>
                  <biblScope unit="volume">20</biblScope>
                  <biblScope unit="issue">1</biblScope>
                  <biblScope unit="pp">10-25</biblScope>
                  <date type="datePub">2024-01-01</date>
                </imprint>
              </monogr>
              <series/>
              <idno type="doi">reg</idno>
              <idno type="arxiv">ger</idno>
              <idno type="bibcode">erg</idno>
              <idno type="ird">greger</idno>
              <idno type="pubmed">greger</idno>
              <idno type="ads">gaergezg</idno>
              <idno type="pubmedcentral">gegzefdv</idno>
              <idno type="irstea">vvxc</idno>
              <idno type="sciencespo">gderg</idno>
              <idno type="oatao">gev</idno>
              <idno type="ensam">xcvcxv</idno>
              <idno type="prodinra">vxcv</idno>
              <ref type="publisher">https://publisher.com/ID</ref>
              <ref type="seeAlso">https://link1.com/ID</ref>
              <ref type="seeAlso">https://link2.com/ID</ref>
              <ref type="seeAlso">https://link3.com/ID</ref>
            </biblStruct>
          </sourceDesc>
          <profileDesc>
            <textClass>
              <keywords scheme="author">
                <term xml:lang="en">keyword1</term>
                <term xml:lang="en">keyword2</term>
                <term xml:lang="fr">mot-clé1</term>
                <term xml:lang="fr">mot-clé2</term>
              </keywords>
              <classCode scheme="halDomain" n="physics"/>
              <classCode scheme="halDomain" n="halDomain2"/>
              <classCode scheme="halTypology" n="ART"/>
            </textClass>
          </profileDesc>
        </biblFull>
      </listBibl>
    </body>
    <back>
      <listOrg type="structures">
        <org type="institution" xml:id="localStruct-affiliation">
          <orgName>laboratory for MC, university of Yeah</orgName>
          <orgName type="acronym">LMC</orgName>
          <desc>
            <address>
              <addrLine>Blue street 155, 552501 Olso, Norway</addrLine>
              <country key="LS">Lesotho</country>
            </address>
            <ref type="url" target="https://lmc.univ-yeah.com"/>
          </desc>
        </org>
        <org type="institution" xml:id="localStruct-affiliationB">
          <orgName>laboratory for MCL, university of Yeah</orgName>
          <orgName type="acronym">LMCL</orgName>
          <desc>
            <address>
              <addrLine>Blue street 155, 552501 Olso, Norway</addrLine>
              <country key="NO">Norway</country>
            </address>
            <ref type="url" target="https://lmcl.univ-yeah.com"/>
          </desc>
        </org>
      </listOrg>
    </back>
  </text>
</TEI>


正確答案


看看https://www.php.cn/link/e1ff36b97044a1c7c73c73e4d27aeba4,你基本上應該使用

tei_namespace = "http://www.tei-c.org/ns/1.0"
tei = "{%s}" % tei_namespace

nsmap = {none : tei_namespace} # the default namespace (no prefix)

root = etree.element(tei + "tei", nsmap=nsmap) # lxml only!
text = etree.subelement(root, tei + "text")

對所有元素依此類推,以確保它們是在 tei 命名空間中創建的。

在記憶體中創建的 elementtree 對架構有效(在我將其與導入的 w3c xml.xsd 一起下載後)是例如

from lxml import etree

TEI_NAMESPACE = "http://www.tei-c.org/ns/1.0"
TEI = "{%s}" % TEI_NAMESPACE

NSMAP = {None : TEI_NAMESPACE} # the default namespace (no prefix)

root = etree.Element(TEI + "TEI", nsmap=NSMAP) # lxml only!
text = etree.SubElement(root, TEI + "text")
body = etree.SubElement(text, TEI + "body")
listBibl = etree.SubElement(body, TEI + "listBibl")
biblFull = etree.SubElement(listBibl, TEI + "biblFull")
sourceDesc = etree.SubElement(biblFull, TEI + "sourceDesc")
profileDesc = etree.SubElement(biblFull, TEI + "profileDesc")


xmlschema_doc = etree.parse("aofr.xsd")
xmlschema = etree.XMLSchema(xmlschema_doc)
# run check
status = xmlschema.validate(root)

print(status)

print(xmlschema.error_log)

以上是無法使用架構驗證 XML,但可以透過從中讀取寫入的檔案來運作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除