验证 xml 元素的属性值

发布于 2024-11-19 20:24:17 字数 389 浏览 7 评论 0原文

我正在尝试编写一个 xsd 文档来验证以下 xml 片段。

<parentElement>
    <someElement name="somethingRequired"/>
    <someElement name="somethingElseRequired"/>
    <someElement name="anything"/>
</parentElement>

如果parentElement包含至少两次出现的someElement,则应进行验证,其中一个具有包含值“somethingRequired”的属性名称,另一个具有包含值“somethingElseRequired”的属性名称。

这可能吗?

I am trying to write a xsd document that validates the following xml snippet.

<parentElement>
    <someElement name="somethingRequired"/>
    <someElement name="somethingElseRequired"/>
    <someElement name="anything"/>
</parentElement>

It shall validate, if parentElement contains at least two occurences of someElement where one has an attribute name containing the value "somethingRequired" and the other has an attribute name containing the value "somethingElseRequired".

Is that possible?

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

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

发布评论

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

评论(2

盗梦空间 2024-11-26 20:24:17

这可能吗?

这取决于您的限制需要有多具体。如果所有 name 属性都具有唯一值就足够了,那么您可以使用 来实现这一点。

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="parentElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="2" maxOccurs="unbounded" name="someElement">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniqueName">
      <xs:selector xpath="someElement" />
      <xs:field xpath="@name" />
    </xs:unique>
  </xs:element>
</xs:schema>

您还可以限制属性值,例如某些允许值的枚举集,而不是仅使用 type="xs:string"。但不可能仅限制前两个名称属性是唯一的,因为不允许 xpath 属性包含谓词。

如果您需要第一个 name 属性具有某个特定值,第二个 name 属性具有其他特定值,其余属性具有任何值,那么我会说这违反了架构组件约束 元素声明一致或者需要某种比更具体的共现约束 所以一般来说这是不可能的。您可以通过在实例文档中使用 xsi:type 属性来显式声明元素的类型。

Is that possible?

It depends on how specific your restrictions need to be. If it is good enough that all of the name attributes have unique values, then you can achieve this with <xs:unique>

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="parentElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="2" maxOccurs="unbounded" name="someElement">
          <xs:complexType>
            <xs:attribute name="name" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniqueName">
      <xs:selector xpath="someElement" />
      <xs:field xpath="@name" />
    </xs:unique>
  </xs:element>
</xs:schema>

You can also restrict the attribute values for example to some enumerated set of allowed values instead of just using type="xs:string". It is not possible though to restrict only the first two name attributes to be unique because the xpath attribute is not allowed to contain predicates.

If you need the first name attribute to have some specific value, the second one some other specific value and the rest to have any value, then I would say that this either violates the Schema Component Constraint Element Declarations Consistent or would need some sort of co-occurrence constraint that is more specific than <xs:unique> so generally it wouldn't be possible. You might be able to make it possible by using xsi:type attribute in the instance document to explicitly declare the type of the element.

够运 2024-11-26 20:24:17

除了根据属性值进行验证之外,您想做的一切都是可能的。

Everything you want to do is possible except for validating based on the attribute values.

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