定义一个 XSD 元素,它可以是日期时间或带有属性的空元素 [英] Define an XSD element which can be a dateTime or empty with an attribute

查看:25
本文介绍了定义一个 XSD 元素,它可以是日期时间或带有属性的空元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题几乎与this一个,但用于 xs:dateTime 类型而不是用户定义的元素.

My question is almost exactly the same as this one, but for a xs:dateTime type rather than a user defined element.

我的 XML 中的元素(不是我创建的)可能如下所示:

An element in my XML (which I do not create) can look like:

<parent>
    ...
    <start>2012-01-01T00:00:00.000</start>
    <end>2013-01-01T00:00:00.000</end>
    ...
</parent>

-或-

<parent>
    ...
    <start reference="a string" />
    <end reference="a string" />
    ...
</parent>

换句话说,在父元素中,开始"和结束"字段可以包含一个 xs:dateTime 值,或者为空但具有引用"属性(任一字段可能是一个或另一个在父级中,它们不一定既是引用也不是日期时间).我已经尝试了各种方法来在 XSD 中表示这一点,但还没有找到解决方案.我最接近的是(摘自一个更大的 XSD):

In other words, within the parent element, the "start" and "end" fields can contain either a xs:dateTime value, or will be empty but have a "reference" attribute (either field might be one or the other within the parent, they're not necessarily both a reference or both a dateTime). I've tried all sorts of ways to represent this in an XSD, but have not found a solution. The closest I've come is (excerpted from a much larger XSD):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="DateOrRef">
    <xs:simpleContent>
<!--      <xs:extension base="xs:dateTime"> This does not validate-->
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="reference" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="parent">
    <xs:sequence>
        <xs:element minOccurs="0" name="start" type="DateOrRef" />
        <xs:element minOccurs="0" name="end" type="DateOrRef" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

会验证,但不会将节点内容限制为 xs:dateTime 值.如果我将扩展基本类型更改为 xs:dateTime 而不是 xs:string,如注释掉的行中那样,空元素将不再有效,因为 dateTime 类型不允许为空.

which does validate, but does not restrict the node contents to be an xs:dateTime value. If I change the extension base type to a xs:dateTime instead of xs:string as in the commented out line, the empty elements will no longer validate because the dateTime type is not allowed to be empty.

我如何构建 XSD 以将这些字段验证为 xs:dateTime 而不是 xs:string?

How could I structure the XSD to validate these fields as xs:dateTime instead of xs:string?

推荐答案

1 您可以将元素声明为 nillable(按照 Ben 的建议).

1 You can declare the element nillable (as suggested by Ben).

2 您可以声明一个简单类型,它是 xs:dateTime 和空字符串的联合.如果我们分阶段构建它,这是最容易遵循的.首先,声明一个命名的简单类型,它的唯一值是空字符串:

2 You can declare a simple type which is a union of xs:dateTime and the empty string. This is easiest to follow if we build it up in stages. First, declare a named simple type whose sole value is the empty string:

<xs:simpleType name="empty-string">
  <xs:restriction base="xs:string">
    <xs:enumeration value=""/>
  </xs:restriction>
</xs:simpleType>

然后声明一个联合类型,其成员为 xs:dateTime 和空字符串:

Then declare a union type whose members are xs:dateTime and empty-string:

<xs:simpleType name="dateTime-or-nothing">
  <xs:union memberTypes="xs:dateTime empty-string"/>
</xs:simpleType>

然后使用 dateTime-or-nothing 作为扩展步骤的基础:

Then use dateTime-or-nothing as the basis for your extension step:

<xs:complexType name="DateOrRef">
  <xs:simpleContent>
    <xs:extension base="dateTime-or-nothing">
      <xs:attribute type="xs:string" 
                    name="reference" 
                    use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

如果引用属性应该在且仅当元素没有内容时出现,那么您将需要一个 XSD 1.1 断言来实现该效果(或辅助 Schematron 架构,或应用层的临时验证代码).XSD 内容模型使您可以轻松地说必须具有日期时间或引用,但前提是这些选项中的每一个都由子元素表示.

If the reference attribute should occur if and only if the element has no content, then you'll need an XSD 1.1 assertion to that effect (or an ancillary Schematron schema, or ad-hoc validation code at the application layer). XSD content models make it easy to say you must have either a dateTime or a reference, but only if each of those options is represented by a child element.

这篇关于定义一个 XSD 元素,它可以是日期时间或带有属性的空元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆