简体   繁体   English

在没有XSD的情况下验证XML

[英]Validate XML without XSD

I have an XML file coming in and need that to have a few specific tags without that I cannot process that file. 我有一个XML文件,需要它有一些特定的标签,否则我将无法处理该文件。 How can I make sure if those tags are there or not , I tried using the XSD validation but file format keeps changing and they keep sending additional tags which I do not need to process the file , but having those additional tags does not harm my process. 我如何确定这些标签是否存在,我尝试使用XSD验证,但是文件格式不断变化,并且它们不断发送不需要处理文件的其他标签,但是拥有这些其他标签不会损害我的流程。

Is there a way to write the XSD in a way that it only looks for a few tags and ignore the others? 有没有一种方法可以编写XSD,使其仅查找几个标签而忽略其他标签?

You can create an xsd in which you have all of the elements you require. 您可以创建一个xsd,其中包含您需要的所有元素。 By default an element has minOccurs=1 , which would imply that it's required. 默认情况下,元素的minOccurs=1 ,这意味着它是必需的。 Then in order to ignore all of the rest you need to add <xs:any processContents="lax" macOccurs="unbounded"/> , which basically says that the xml may contain any number of additional elements which do not need to be validated. 然后,为了忽略所有其余部分,您需要添加<xs:any processContents="lax" macOccurs="unbounded"/> ,这基本上表明xml可以包含任意数量的不需要验证的其他元素。

Consider forgoing an XSD and instead writing XPath checks against the XML to test known-invariant properties of your XSD. 考虑放弃XSD,而是针对XML编写XPath检查,以测试XSD的已知不变属性。 XSD is better for when you have a known, relatively static grammar. 当您拥有已知的,相对静态的语法时,XSD更好。 Ad hoc XPath assertions or Schematron would be better for XML that can't be held do a definitive grammar. 临时XPath断言或Schematron对于无法保存确定性语法的XML会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM