1

我想知道是否可以检查/捕获“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>

提前感谢您的建议。

4

2 回答 2

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>

于 2013-02-06T08:56:18.670 回答
0

为什么不通过使用文件集来简化解决方案(而不是使用第 3 方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.
于 2013-01-29T20:17:25.257 回答