验证 XML 模式中不同的所需属性集

发布于 2024-09-29 18:52:50 字数 832 浏览 7 评论 0原文

我的要求是使用两种不同的验证策略来验证 xml。在策略 1 中,xml 需要具有具有某些所需属性的元素实例。在策略 2 中,xml 需要具有具有不同所需属性的同一元素的实例。因此,其想法是,同一元素有两个不同的集合,每个集合具有不同的所需属性集合。有没有一种方法可以仅使用一个 xsd 来验证这一点。到目前为止我唯一的解决方案是使用两个不同的 xsd 文件进行验证。

例1:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element firstName="something"/> <!--required -->
    <element lastName="something"/> <!-- required -->
 <element phoneNumber="something"/> <!-- not required -->
<root>

例2:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element firstName="something"/> <!-- not required -->
    <element lastName="something"/> <!-- required -->
 <element phoneNumber="something"/> <!-- required -->
<root>

能否实现xsd验证?

My requirement is to validate an xml with two different validation strategies. In strategy 1, the xml is required to have instances of an element with certain required attributes. In strategy 2, the xml is required to have instances of the same element with different required attributes. So, the idea is that there are two different sets of the same element with different set of required attributes for each set. Is there a way to validate this with using only one xsd. My only solution so far is to use two different xsd files to validate.

Example 1:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element firstName="something"/> <!--required -->
    <element lastName="something"/> <!-- required -->
 <element phoneNumber="something"/> <!-- not required -->
<root>

Example 2:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <element firstName="something"/> <!-- not required -->
    <element lastName="something"/> <!-- required -->
 <element phoneNumber="something"/> <!-- required -->
<root>

Can the validation by xsd be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2024-10-06 18:52:50

Rick,

我在我们的应用程序中遇到了类似的问题需要解决,我们最好的解决方案是使用模块化模式。

因此,Schema1 和 Schema2 不是彼此重复(除了不同的部分),而是让 Schema1 和 Schema2 仅包含不同的约束,并且都导入定义共享约束的通用 Schema3。

如何执行此操作的示例位于此处

它不会为您提供单个 XSD,但它确实减少了重复,因此它有助于提高可维护性,并有助于避免当 Schema1 和 Schema2 之间存在大量重复时出现错误的可能性。

缺点是模式对于人类来说变得有点难以遵循,因为定义被分散在不同的文件之间。

Rick,

I've had similar problems to solve in our apps, and our best solution has been to use modularized schemas.

So instead of having Schema1 and Schema2 that are duplicates of each other except for the part that's different, you have Schema1 and Schema2 that include only the constraints that are different, and both import a common Schema3 that defines the constraints that are shared.

An example of how to do that is here.

It doesn't give you a single XSD, but it does reduce duplication, so it helps with maintainability and helps avoid the likelihood of errors creeping in when you have much duplication between Schema1 and Schema2.

The disadvantage is that the schemas become a little more difficult for humans to follow, because the definitions are split up between different files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文