Question

Sorry if this is simple or already answered somewhere - I couldn't find anything useful with any of the search terms I tried.

I have some date input, that I would like to make in a conditional cascade, like this:

  1. Entire date is in ISO format order - YYYY-MM-DD
  2. Month and day may contain 'illegal' values, in particular, zero or Feb. 30
  3. Key (and the reason for this post), if month is present, year must be present; if day is present, month and year must be present. So effectively, the date may be 'YYYY' or 'YYYY-MM' or 'YYYY-MM-DD'. What I have so far is just a simple specification that checks the individual elements, but not a joined plan as I have described. Is this possible with XSD?

    <xs:element name='DatumOd' minOccurs='0' maxOccurs='1'>
      <xs:simpleType>
        <xs:restriction base='xs:string'>
          <xs:minLength value='4'/>
          <xs:maxLength value='10'/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name='RokStary' minOccurs='0' maxOccurs='1'>
      <xs:simpleType>
        <xs:restriction base='xs:short'>
          <xs:minInclusive value='700' />
          <xs:maxInclusive value='2013' />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name='MesicStary' minOccurs='0' maxOccurs='1'>
      <xs:simpleType>
        <xs:restriction base='xs:byte'>
          <xs:minInclusive value='0' />
          <xs:maxInclusive value='12' />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name='DenStary' minOccurs='0' maxOccurs='1'>
      <xs:simpleType>
        <xs:restriction base='xs:byte'>
          <xs:minInclusive value='0' />
          <xs:maxInclusive value='31' />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    

==> Infant

That would be one approach, but it requires all the date information in one element. Also, it allow many more nonsense values in my date numbers, like year 2999, months 13, 14..., days up to 39.

If you look again at my attempt, you'll see that I either have all the data info at once, in the element: [DatumOd], or in three seperate elements: [RokStary], [MesicStary] and [DenStary]. What I would like to do is specifiy that I can accept either [DatumOd], or the set [RokStary], [MesicStary] and [DenStary]. Additionally, I would like to cascade the dependence, so that the presence of [MesicStary] requires that [RokStary] be present, and [DenStary] requires that [MesicStary] be present. So, I could have:

[DatumOd]

OR

[RokStary]

OR

[RokStary] AND [MesicStary]

OR

[RokStary] AND [MesicStary] AND [DenStary].

Does this make it clearer?

========================================================

Sample data

    <?xml version='1.0' encoding='UTF-8'?>
    <dataroot>
      <DSXML SeqNum='1' ID='92491'>
        <TypZaznamu>Z</TypZaznamu>
        <Rad>RO</Rad>
        <Druh>Spermophilus citellus</Druh>
        <CharVyskyt>0</CharVyskyt>
        <DatumOd>1894</DatumOd>
        <Katastr>Habartice</Katastr>
        <Kvadrat>4956</Kvadrat>
        <Oblast>Frydlantska pahorkatina</Oblast>
        <Zdroj>(Wurm 1894, Wiesbaur 1894)</Zdroj>
        <Pocet>0</Pocet>
        <Elevation>240</Elevation>
        <Zapsano>06/24/98</Zapsano>
        <Zapsal>M.A.</Zapsal>
        <Poznamka>(Ebersdorf)</Poznamka>
        <Okres>LI</Okres>
        <Region>SC</Region>
      </DSXML>
      <DSXML SeqNum='2' ID='92591'>
        <TypZaznamu>Z</TypZaznamu>
        <Rad>RO</Rad>
        <Druh>Spermophilus citellus</Druh>
        <CharVyskyt>0</CharVyskyt>
        <DatumOd>1894-09</DatumOd>
        <Katastr>Habartice</Katastr>
        <Kvadrat>4956</Kvadrat>
        <Oblast>Frydlantska pahorkatina</Oblast>
        <Zdroj>(Wurm 1894, Wiesbaur 1894)</Zdroj>
        <Pocet>0</Pocet>
        <Elevation>240</Elevation>
        <Zapsano>06/24/98</Zapsano>
        <Zapsal>M.A.</Zapsal>
        <Poznamka>(Ebersdorf)</Poznamka>
        <Okres>LI</Okres>
        <Region>SC</Region>
      </DSXML>
      <DSXML SeqNum='3' ID='93491'>
        <TypZaznamu>Z</TypZaznamu>
        <Rad>RO</Rad>
        <Druh>Spermophilus citellus</Druh>
        <CharVyskyt>0</CharVyskyt>
        <DatumOd>1894-03-22</DatumOd>
        <Katastr>Habartice</Katastr>
        <Kvadrat>4956</Kvadrat>
        <Oblast>Frydlantska pahorkatina</Oblast>
        <Zdroj>(Wurm 1894, Wiesbaur 1894)</Zdroj>
        <Pocet>0</Pocet>
        <Elevation>240</Elevation>
        <Zapsano>06/24/98</Zapsano>
        <Zapsal>M.A.</Zapsal>
        <Poznamka>(Ebersdorf)</Poznamka>
        <Okres>LI</Okres>
        <Region>SC</Region>
      </DSXML>
      <DSXML SeqNum='4' ID='9418'>
        <TypZaznamu>Z</TypZaznamu>
        <Rad>CA</Rad>
        <Druh>Neovison vison</Druh>
        <CharVyskyt>2</CharVyskyt>
        <RokStary>1991</RokStary>
        <MesicStary>01</MesicStary>
        <DenStary>19</DenStary>
        <Katastr>Cernousy</Katastr>
        <Kvadrat>4956</Kvadrat>
        <Oblast>Frydlantska pahorkatina</Oblast>
        <Zdroj>(Andera &amp; Hanzal 1996)</Zdroj>
        <Pocet>0</Pocet>
        <Elevation>260</Elevation>
        <Zapsano>03/10/99</Zapsano>
        <Zapsal>M.A.</Zapsal>
        <Okres>LI</Okres>
        <Region>SC</Region>
      </DSXML>
      <DSXML SeqNum='5' ID='94174'>
        <TypZaznamu>Z</TypZaznamu>
        <Rad>CA</Rad>
        <Druh>Neovison vison</Druh>
        <CharVyskyt>2</CharVyskyt>
        <RokStary>1991</RokStary>
        <Katastr>Habartice</Katastr>
        <Kvadrat>4956</Kvadrat>
        <Oblast>Frydlantska pahorkatina</Oblast>
        <Zdroj>(Andera &amp; Hanzal 1996)</Zdroj>
        <Pocet>0</Pocet>
        <Elevation>240</Elevation>
        <Zapsano>03/10/99</Zapsano>
        <Zapsal>M.A.</Zapsal>
        <Okres>LI</Okres>
        <Region>SÈ</Region>
      </DSXML>
      <DSXML SeqNum='6' ID='94660'>
        <TypZaznamu>Z</TypZaznamu>
        <Rad>RO</Rad>
        <Druh>Apodemus agrarius</Druh>
        <CharVyskyt>4</CharVyskyt>
        <RokStary>1991</RokStary>
        <MesicStary>05</MesicStary>
        <Katastr>Cernousy</Katastr>
        <Kvadrat>4956</Kvadrat>
        <Oblast>Frydlantska pahorkatina</Oblast>
        <Zdroj>(Barta Z. &amp; Benda 1998)</Zdroj>
        <Pocet>9</Pocet>
        <Elevation>260</Elevation>
        <Zapsano>05/06/99</Zapsano>
        <Zapsal>M.A.</Zapsal>
        <Okres>LI</Okres>
        <Region>SC</Region>
      </DSXML>
    </dataroot>
Was it helpful?

Solution

Assuming sample input xml:

<root>
  <datevalue>2013-05-29</datevalue>
</root>

sample XSD code would be like:

<xs:element name="root">
  <xs:complexType>
    <xs:element name='datevalue' minOccurs="0">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="[0-2][0-9][0-9][0-9]
                            |[0-2][0-9][0-9][0-9][-][01|02|03|04|05|06|07|08|09|10|11|12]
                            |[0-2][0-9][0-9][0-9][-][01|02|03|04|05|06|07|08|09|10|11|12][-][01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31]"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:complexType>
</xs:element>

Values can be either:

0000 to 2999
or 0000-01 to 2999-12
or 0000-01-01 to 2999-12-31

Since you say that there can be potential non-realistic values and we have to accept it as is.. I have one more approach for you, which has quite simpler code:

<xs:element name="root">
  <xs:complexType>
    <xs:element name='datevalue' minOccurs="0">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="[0-2][0-9][0-9][0-9]
                            |[0-2][0-9][0-9][0-9][-][0-1][0-9]
                            |[0-2][0-9][0-9][0-9][-][0-1][0-9][-][0-3][0-9]"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:complexType>
</xs:element>

Values can be either:

0000 to 2999
or 0000-00 to 2999-12 (to 2999-19)
or 0000-00-00 to 2999-12-31 (to 2999-19-39) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top