需要空字符或2个字符的XSD SimpleType

时间:2019-06-18 11:33:09

标签: xml xsd xsd-validation

我试图弄清楚如何更改需要节点“ Initials”为空或仅2个字符的XSD。

const rover1 = {
  direction: 'N',
  x : 0, 
  y : 0,
};

const rover2 = {
  direction: 'N',
  x : 5, 
  y : 7,
};

let travelLog = {
        rover1: [],
        rover2: [],

}


const obstacles = {
  x: [0,1,2,2,4,6,7],
  y: [6,0,2,7,4,3,8],

};


    

// TURNLEFT FUNCTION

function turnLeft(rover){
  console.log("turnLeft was called!");
  switch(rover.direction) {
    case 'N' : 
    rover.direction = 'W'; 
    break; 
    case 'W' : 
    rover.direction = 'S'; 
    break; 
    case 'S' : 
    rover.direction = 'E'; 
    break; 
    case 'E' : 
    rover.direction = 'N'; 
    break; 
  }
};

// TURN RIGHT FUNCTION 

function turnRight(rover){
  console.log("turnRight was called!");

  switch (rover.direction) {
    case 'N' : 
    rover.direction = 'E'; 
    break;
    case 'E' : 
    rover.direction = 'S'; 
    break; 
    case 'S' : 
    rover.direction = 'W';
    break; 
    case 'W' :
    rover.direction = 'N'; 
    break; 
  }
};

// FUNCTION TO CHECK IF THERE IS ANY OBSTACLE AHEAD.

function checkObstacle (rover,otherRover){
  for (let i = 0; i < obstacles.x.length; i++){
    if(obstacles.x[i] === rover.x && obstacles.y[i] === rover.y){
      console.log('An obstacle was found! Order aborted!');
      return true; 
    } else if(otherRover.x=== rover.x && otherRover.y === rover.y){
      console.log('Another rover found in your path! Order aborted!');
      return true; 
    } else {
      return false; 
    }
  }
};

// MOVE FORWARD FUNCTION

function moveForward (rover) {
    console.log("moveForward was called");
    travelLog[rover].push(['[x' + rover.x +',' + 'y' + rover.y + ']' ]);
    
    if(rover.direction === 'N' && rover.y > 0 ){ // NORTHBOUND MOVEMENT
        rover.y--;
        if(checkObstacle()){
          rover.y++;
        };
       

    } else if(rover.direction === 'W' && rover.x > 0){ // WESTBOUND MOVEMENT
      rover.x--;
      if(checkObstacle()){
        rover.x++;
      };
    

    } else if(rover.direction === 'S' && rover.y < 9 ){ // SOUTHBOUND MOVEMENT
      rover.y++;
      if(checkObstacle()){
        rover.y--;
      };
    
  } else if(rover.direction ==='E' && rover.x <9){ //EASTBOUND MOVEMENT
        rover.x++; 
        if(checkObstacle()){
          rover.x--;
        };
              
    } else {
      console.log('Your order tried to put the rover off the grid! Order cancelled!');
    };


    console.log(`The new position of the rover is heading ${rover.direction} row ${rover.x} column ${rover.y}`);
}

我应该做到可口吗?

2 个答案:

答案 0 :(得分:1)

您可以使用模式值([A-Z][A-Z])?

答案 1 :(得分:1)

带有类似XML的示例

<root>AZ</root>

<root></root>

您的XSD可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="root" type="Initials" />

  <xs:simpleType name="Initials">
    <xs:restriction base="xs:string">
      <xs:pattern value="|[A-Z]{2}"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

xs:pattern确实匹配零长度字符串或两个字符的A-Z字符串。删除<xs:maxLength value="2"/><xs:minLength value="2"/>对于照顾零长度的字符串很有必要。