我当前正在使用 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中文网其他相关文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

记事本++7.3.1
好用且免费的代码编辑器

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中