简体   繁体   English

如何更改Ant的XMLValidate任务提供的验证的“级别”?

[英]How do I change the “level” of validation that Ant's XMLValidate task provides?

I am attempting to use Ant's XMLValidate task to validate an XML document against a DTD. 我试图使用Ant的XMLValidate任务针对DTD验证XML文档。 The problem is not that it doesn't work, but that it works too well. 问题不是它不起作用,而是效果很好。 My DTD contains an xref element with an "@linkend" attribute of type IDREF. 我的DTD包含一个xref元素,该元素具有IDREF类型的“ @linkend”属性。 Most of these reference IDs outside of the current document. 这些参考ID大多位于当前文档之外。 Because of this, my build fails, since the parser complains that the ID that the IDREF is referencing doesn't exist. 因此,我的构建失败,因为解析器抱怨IDREF所引用的ID不存在。 So, is there any way that I can validate my XML document against the DTD, but ignore errors of this type? 因此,有什么方法可以针对DTD验证我的XML文档,但是可以忽略此类错误?

A few things I've tried: Setting the "lenient" option on XMLValidate makes the task only check the document's well-formedness, not it's validity against a DTD. 我尝试过的一些事情:在XMLValidate上设置“ lenient”选项使该任务仅检查文档的格式正确,而不是针对DTD的有效性。 The XMLValidate task in the Ant manual lists some JAXP and SAX options you can set, but none seem applicable. Ant手册的XMLValidate任务列出了您可以设置的一些JAXP和SAX选项,但似乎都不适用。

Here's my code: 这是我的代码:

 <target name="validate">
        <echo message="Validating ${input}"/>
        <xmlvalidate file="${input}" failonerror="yes"
            classname="org.apache.xml.resolver.tools.ResolvingXMLReader">
            <classpath refid="xslt.processor.classpath"/>
        </xmlvalidate>
 </target>

As you can see, I'm using ResolvingXMLReader to resolve the DTD against a catalog of public identifiers. 如您所见,我正在使用ResolvingXMLReader根据公共标识符目录来解析DTD。 However, I get the same behavior if I specify the DTD directly using a nested xmlcatalog element. 但是,如果我直接使用嵌套的xmlcatalog元素指定DTD,则会得到相同的行为。

Your problem derives from the difference between two interpretations of the DTD: yours, and the spec's :-). 您的问题源于DTD的两种解释:您的解释和规范的 :-)之间的差异。 IDREFs must refer to ids in the same document, whereas yours refer to elements across documents. IDREF必须引用同一文档中的ID,而IDREF必须引用文档中的元素。

My suggestion is to create your own version of the DTD that specifies NMTOKEN instead of IDREF for that attribute, and use it to perform your validation. 我的建议是创建自己的DTD版本,为该属性指定NMTOKEN而不是IDREF,然后使用它来执行验证。 This will ensure that the contents will be valid xml id values. 这将确保内容为有效的xml id值。

Not sure if this helps, but could you try this workaround? 不知道这是否有帮助,但是您可以尝试这种解决方法吗? Create a temporary file, merge all your XMLs, and do the validation. 创建一个临时文件,合并所有XML,然后进行验证。

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

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