Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

c# - Allow XSD date element to be empty string

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

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>

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...