【问题标题】:Allowing 2 date types for an attribute in an XML schema允许 XML 模式中的属性使用 2 种日期类型
【发布时间】:2022-02-11 17:22:58
【问题描述】:

我编写了一个 XML 模式,其中一个元素是日期。下面是定义日期的架构部分

<xs:element name="temporal">
    <!--start date and end date-->
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" name="date">
                <xs:complexType>
                    <xs:attribute name ="date" type ="xs:date" use="required"/>
                    <xs:attribute name="dateType" type="xs:string" use="required"/>
                    <xs:attribute name="dateFormat" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

对于属性

我希望能够拥有 type = "xs:date" 或 type = "xs:gYear" 因为某些集合不包含完整的 YYYY-MM-DD 格式。我尝试做:

<xs:element name="date">
    <xs:simpleType>
        <xs:union memberTypes="xs:date xs:gYear" />
    </xs:simpleType>
</xs:element>

但这不起作用,当我导出 excel 表单时,YYYY 日期存储为 1905-02-04。我真的不知道是否可以使用 xml 模式使用“或”选项?还是允许选择两种日期类型而不必具有两个单独元素的解决方案?

任何建议或解决方案都会有所帮助

【问题讨论】:

  • 我很困惑,如果它是一个名为“date”的属性有这两种可能的格式,你为什么要为一个名为“data”的元素定义一个联合类型?
  • 抱歉打错字了,应该是日期
  • 是的,但是你为什么要定义一个元素而不是一个属性呢?我本来希望看到一个命名简单类型的定义,属性声明包含对命名类型的引用。

标签: xml date schema


【解决方案1】:

我想你想要

<xs:simpleType name="date-or-gYear">
    <xs:union memberTypes="xs:date xs:gYear" />
</xs:simpleType>

然后

<xs:attribute name="date" type ="date-or-gYear" use="required"/>
                

【讨论】:

  • 啊,谢谢,我会试试看是否有效。
  • 嗨,Michael,我应该在架构的什么位置定义 simpleType?
  • 这样的问题让我很担心,这有点像在飞机上问如何启动引擎——如果你不知道,这里不是你想知道的地方。如果您正在编写 XSD(或任何其他语言),则需要从某种教程开始,它将回答这些问题。但答案是它可以作为 xs:schema 的子项出现在任何地方——尽管您可能需要了解 targetNamespace 的概念。
猜你喜欢
  • 2019-12-17
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 2019-07-11
  • 2021-04-04
  • 2013-11-15
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多