我想知道是否可以检查/捕获" schemavalidate" /" xmlvalidate"的结果。操作
想法是解析文件夹中的文件,尝试针对XSD验证每个文件并获取属性中的操作状态(最终,在日志文件中输出操作的状态和结果) 。
然后可以检查验证的状态,以便知道是否应该对该特定XML文件执行某些其他任务。
例如:
<target name="convert-user-folder">
<echo>${user.folder}</echo>
<!-- Iterate all XML files in the folder -->
<foreach target="validate-xml-file" param="user.input.xml">
<path>
<fileset dir="${user.folder}">
<include name="*.xml" />
</fileset>
</path>
</foreach>
</target>
<target name="validate-xml-file">
<echo message="Validating ${user.input.xml}"/>
<!-- Checking if XML is well formed -->
<echo message="Checking if ${user.input.xml} is well formed"/>
<xmlvalidate file="${user.input.xml}" failonerror="false" lenient="true"/>
<!-- HOW WE COULD CHECK THE RESULT OF THE VALIDATION OPERATIONS
WITHOUT EXITING ?-->
<!-- Checking if file validates against XSD -->
<echo message="Checking if ${user.input.xml} validates against schema"/>
<schemavalidate noNamespaceFile="${xsds.dir}/userInput.xsd"
file="${user.input.xml}" failonerror="false"/>
<!-- HOW WE COULD CHECK THE RESULT OF THE VALIDATION OPERATIONS
WITHOUT EXITING? -->
<!-- HERE WE SHOULD GET IN A PROPERTY THE STATUS OF THE OPERATION AND WRITE
IN A FILE SOMETHING LIKE : "OPERATION STATUS: SUCCESS/FAILURE: The reason
was: something from the schemavalidate output" -->
<!-- IF THE OPERATION WAS SUCCESSFUL WE SHOULD CALL SOME TASKS OTHERWISE
CALL OTHER TASKS -->
</target>
提前感谢您的建议。
答案 0 :(得分:1)
我实际需要的是来自antcontrib的trycatch。这解决了我的问题,如下面的代码段所示。
<target name="validate-xml-file">
<echo message="Validating ${user.input.xml}"/>
<!-- Checking if XML is well formed -->
<echo message="Checking if ${user.input.xml} is well formed"/>
<trycatch property="xml.well.formed.result">
<try>
<xmlvalidate file="${user.input.xml}" failonerror="true" lenient="true"/>
</try>
<catch/>
<finally/>
</trycatch>
<!-- Checking if file validates against XSD -->
<echo message="Checking if ${user.input.xml} validates against schema"/>
<trycatch property="schema.validation.result">
<try>
<schemavalidate noNamespaceFile="${xsds.dir}/userInput.xsd"
file="${user.input.xml}" failonerror="true"/>
</try>
<catch/>
<finally/>
</trycatch>
<!-- Create two properties for the results of the validation -->
<condition property="xml.well.formed.result.set" else="false">
<isset property="xml.well.formed.result"/>
</condition>
<condition property="schema.validation.result.set" else="false">
<isset property="schema.validation.result"/>
</condition>
<!-- Here we can write into a HTML format the result of the operation according
to the xml.well.formed.result, schema.validation.result a.s.o.) -->
<!-- Also, perform something according to the validation or not of the XML -->
<if>
<or>
<equals arg1="${xml.well.formed.result.set}" arg2="true"/>
<equals arg1="${schema.validation.result.set}" arg2="true"/>
</or>
<then>
<!-- Here we call some task -->
</then>
<else>
<!-- Here we call some other task or just fail to fw the failure -->
</else>
</if>
答案 1 :(得分:0)
为什么不使用文件集简化解决方案(而不是使用第三方ant-contrib“foreach”任务)。
<xmlvalidate failonerror="false" ..>
<fileset dir="${user.folder}" includes="*.xml"/>
</xmlvalidate>
<schemavalidate failonerror="false" ..>
<fileset dir="${user.folder}" includes="*.xml"/>
</schemavalidate>
我刚刚测试了一个类似的解决方案并得到以下消息告诉我文件和行抛出了验证问题:
[schemavalidate] /path/to/file/file.xml:119:18: cvc-complex-type.2.4.a: Invalid content was found starting with element 'helloworld'. One of '???' is expected.