Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...