允许XSD日期元素为空字符串 [英] Allow XSD date element to be empty string

查看:93
本文介绍了允许XSD日期元素为空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许一个元素为 xs:date 或一个空字符串。

I would like to allow an element to be a xs:date or an empty string.

这里我尝试过的XML模式:

Here's an XML Schema that I've tried:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:lp="urn:oio:ebst:diadem:lokalplan:1" 
           targetNamespace="urn:oio:ebst:diadem:lokalplan:1" 
           elementFormDefault="qualified" xml:lang="DA" 
           xmlns:m="urn:oio:ebst:diadem:metadata:1">
  <xs:import schemaLocation="../key.xsd" namespace="urn:oio:ebst:diadem:metadata:1" />
  <xs:element name="DatoVedtaget" type="lp:DatoVedtagetType" />
  <xs:complexType name="DatoVedtagetType">
    <xs:simpleContent>      
      <xs:extension base="xs:date">
        <xs:attribute ref="m:key" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="DatoVedtagetTypeString">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute ref="m:key" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

我希望元素为 DatoVedtagetType 一个包含值的案例,如果它为空,我希望它是 DatoVedtagetTypeString 。我如何在这种模式下实现这种条件功能?

I want the element to be DatoVedtagetType in a case it includes a value, and I want it to be DatoVedtagetTypeString if it is empty. How I implement such a conditional functionality this schema?

推荐答案

每个问题的注释目标是使 DatoVedtaget xs:date 或为空。这是表达这种约束的一种方法:

Per comments on the question, the goal is to have DatoVedtaget be a xs:date or empty. Here is a way to express such a constraint:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:lp="urn:oio:ebst:diadem:lokalplan:1"
           xmlns:m="urn:oio:ebst:diadem:metadata:1"
           targetNamespace="urn:oio:ebst:diadem:lokalplan:1"
           elementFormDefault="qualified"
           xml:lang="DA">
  <xs:import schemaLocation="../key.xsd" namespace="urn:oio:ebst:diadem:metadata:1" />
  <xs:element name="DatoVedtaget" type="lp:DatoVedtagetType" />

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

  <xs:simpleType name="dateOrEmpty">
    <xs:union memberTypes="xs:date lp:empty"/>
  </xs:simpleType>  

  <xs:complexType name="DatoVedtagetType">
    <xs:simpleContent>
      <xs:extension base="lp:dateOrEmpty">
        <xs:attribute ref="m:key" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

</xs:schema>

这篇关于允许XSD日期元素为空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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