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

(サードパーティの 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 に答える