如何使xsd gYear元素可选

时间:2016-09-23 20:25:54

标签: xml xsd xsd-validation

有一个xml文件,我希望这两种类型都有效

<xs:element name="releaseYear" type="xs:gYear" minOccurs="0"/> OR
<xs:element name="releaseYear" type="xs:gYear" minOccurs="0" maxOccurs="1"/>   OR
<xs:element name="releaseYear" type="xs:gYear" nillable="true" minOccurs="0"/>  

现在,我在XSD上尝试了以下内容

xs:string

这些都没有奏效。 使XSD验证XML的唯一因素是我将类型设置为<xs:element name="releaseYear" type="xs:string"/> ,如此

#include <iostream>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include "keypad.h"

extern "C" {
#include <pigpio.h>
}

using namespace std;

constexpr unsigned int  keypad::READ_PINS      [3];
constexpr unsigned int  keypad::WRITE_PINS     [8];
constexpr char          keypad::CHAR_MATRIX    [3] [8];
int                     keypad::last_levels    [3] [8];
int                     keypad::current_levels [3] [8];
int                     keypad::current_cycle;
int                     keypad::last_read_pin;
int                     keypad::last_write_pin;
uint32_t                keypad::current_tick;
uint32_t                keypad::last_tick;
key_event_handler       keypad::key_press_event_handler;
key_event_handler       keypad::key_release_event_handler;
timespec               *keypad::mux_cycle_time;
timespec               *keypad::mux_cycle_time_remaining;

void keypad::KeyISR (int pin, int level, uint32_t tick)
{
   int i = 0;
   int j = 0;

   for (i = 0; i < 3; i++) {
     if ((int) READ_PINS [i] == pin) j = i;
   }

  if (level == 1) {
     current_levels [j] [current_cycle] = 1;

     if (last_levels [j] [current_cycle] == 0) {
       if (key_press_event_handler != NULL) {
         if (tick - current_tick < 250) {
           if (last_read_pin != j) last_write_pin = -1;

           if (last_write_pin == -1) {
             key_press_event_handler (CHAR_MATRIX [j] [current_cycle],    current_tick);
           }
        }
        else if (tick - last_tick > 250000) {
          cout << "tick: " << tick << "; current tick: " << current_tick << "; cycle: " << current_cycle << "; j: " << j << "; key: " <<
              CHAR_MATRIX [j] [current_cycle] << endl;
          key_press_event_handler (CHAR_MATRIX [j] [current_cycle], current_tick);
          last_tick = tick;
        }
      }

      last_levels [j] [current_cycle] = 1;
      last_read_pin                   = j;
    }
  }
  else {
    last_levels [j] [current_cycle] =  0;
    last_read_pin                   = -1;
  }
}

void keypad::SetKeyPressEventHandler (key_event_handler handler)
{
   key_press_event_handler = handler;
}

void keypad::SetKeyReleaseEventHandler (key_event_handler handler)
{
   key_release_event_handler = handler;
}

int keypad::Initialize ()
{
  int ret = 0;
  int i   = 0;

  timespec *mux_cycle_time           = new timespec;
  timespec *mux_cycle_time_remaining = new timespec;

  mux_cycle_time->tv_sec            = 0;
  mux_cycle_time->tv_nsec           = 625000000;
  mux_cycle_time_remaining->tv_sec  = 0;
  mux_cycle_time_remaining->tv_nsec = 0;
  current_cycle                     = 0;
  last_read_pin                     = -1;
  last_write_pin                    = -1;
  current_tick                      = (uint32_t) 0;
  last_tick                         = (uint32_t) 0;
  key_press_event_handler           = NULL;
  key_release_event_handler         = NULL;

  cout << "Initializing keypad...";

  for (i = 0; i < 3; i++) {
    gpioSetMode (READ_PINS [i], PI_INPUT);
    gpioSetPullUpDown (READ_PINS [i], PI_PUD_DOWN);
  }

  for (i = 0; i < 8; i++) {
    gpioSetMode (WRITE_PINS [i], PI_OUTPUT);
  }

  ret = gpioSetAlertFunc(22, keypad::KeyISR);

  if (ret != 0) cout << "ERROR on pin 22! Code: " << ret << "." << endl;

  ret = gpioSetAlertFunc(23, keypad::KeyISR);

  if (ret != 0) cout << "ERROR on pin 23! Code: " << ret << "." << endl;

  ret = gpioSetAlertFunc(24, keypad::KeyISR);

  if (ret != 0) cout << "ERROR on pin 24! Code: " << ret << "." << endl;
  else          cout << "done." << endl;

  return ret;
}

void keypad::Run ()
{
  int i   = 0;
  int j   = 0;
  int k   = 0;
  int ret = 0;

  cout << "Keypad is now ready to use. Happy typing! :-)" << endl;

  while (true) {
    if (k >= 7) k = 0;
    else        k++;

    for (j = 0; j < 8; j++) {
      ret = gpioWrite (WRITE_PINS [j], 0);

      if (ret != 0) {
        cout << "RESET ERROR! Pin: " << WRITE_PINS [j] << "; Code: " << ret << endl;
        exit (1);
      }
    }

    for (i = 0; i < 3; i++) {
      current_levels [i] [k] = 0;
    }

    ret = gpioWrite (WRITE_PINS [k], 1);

    if (ret != 0) {
      cout << "WRITE ERROR! Pin: " << WRITE_PINS [k] << "; Code: " << ret << endl;
      exit (1);
    }

    ret = nanosleep (mux_cycle_time, mux_cycle_time_remaining);

    current_cycle  = k;
    last_write_pin = k;
    current_tick   = gpioTick ();
  }
}

void keypad::Terminate ()
{
  cout << "Keypad terminated." << endl;
}

但这不是正确的做法,任何关于让它验证一年或“

的建议

感谢。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

<xs:element name="releaseYear" type="xs:gYear" nillable="true"/> 

以下XML对上述Schema有效。

<releaseYear>2032</releaseYear> or
<releaseYear xsi:nil="true"/>

请参阅XML Schema nillable=”true” vs minOccurs=”0″

答案 1 :(得分:0)

我首选的方法是定义一个列表类型,其中gYear为itemType,minLength=0maxLength=1