首页 > 解决方案 > 使用 Schematron 检查是否在目标 XML 文档中定义了参考 XML 的每个元素

问题描述

我想使用 Schematron 来验证 XML 文档,方法是检查它是否包含来自单独的参考 XML 源的所有必需元素。例如,我有一个 XML 文件requiredIngredients.xml,它定义:

<requiredIngredients>
   <ingredient>Water</ingredient>
   <ingredient>Flour</ingredient>
   <ingredient>Salt</ingredient>
</requiredIngredients>

使用它,我想验证另一个包含以下内容的 XML 文件:

<myRecipe>
   <ingredient>Sugar</ingredient>
   <ingredient>Water</ingredient>
</myRecipe>

在这种情况下,我需要两个单独的错误,一个是缺少“Flour”,另一个是缺少“Salt”。如果我想使用类似...的东西,请使用 Schematron

<rule context="/myRecipe">...</rule>

...我可以使用函数(例如检查)对requiredIngredients.xml文件进行测试,但这只会产生一个错误。我可以通过将自己转换为一组 Schematron 断言来实现这一点,但我真的希望将其分开,因为可能存在大量此类文件(将其视为我的验证规则的输入以及要验证的实际 XML)。documentevery .. in .. satisfiesrequiredIngredients.xml

任何想法都会非常受欢迎!

标签: xmlschematron

解决方案


推荐阅读