views:

44

answers:

1

This XML documentation seems to say that the ID derived type supports a pattern, but when I try to define one with this code:

      <complexType name="CourseType">
          <attribute name="courseNumber" type="ID">
              <pattern value="[A-Z]{2}(\d{3}).(\d{3})" />
          </attribute>
          <attribute name="numOfCredits" type="university:CourseCredits" />
          <element name="course_name" type="university:MixedName" />
          <element name="course_professor" type="string" />
      </complexType>>


...I get an error in the oXygen XML editor that says The content of 'courseNumber' must match (annotation?, (simpleType?)). A problem was found starting at: pattern.

Am I defining my schema correctly for that ID attribute?

+1  A: 

If you need to restrict built-in simple data type you should create your own simpleType. Use Derivation by Restriction. Try something like this:

<simpleType name='better-ID'>
  <restriction base='ID'>
    <pattern value='(\d{3}).(\d{3})'/>
  </restriction>
</simpleType>

<complexType name="CourseType">
      ...
      <attribute name="courseNumber" type="better-ID"/>
      <attribute name="numOfCredits" type="university:CourseCredits" />
</complexType>

Or you can just embed simpleType:

   <complexType name="CourseType">
          ...
          <attribute name="courseNumber">
              <simpleType>
                  <restriction base='ID'>
                     <pattern value='(\d{3}).(\d{3})'/>
                  </restriction>
              </simpleType>
          </attribute>
          <attribute name="numOfCredits" type="university:CourseCredits" />
    </complexType>

See also @jasso comments below to fix some other errors in your XSD.

Shcheklein
@Shcheklein: The idea is correct but this solution has some problems. `complexType` can't contain `element` as a direct child. All `element`s need to be descendants of a single `all`, `choice`, `group` or `sequence` element. Also attributes need to be defined *after* the element container.
jasso
@kchau, @Shcheklein: The pattern for ID that was specified by OP, seems weird as well. IIRC \d means decimal number so it can contain a dot. Thus the ID contains only numbers and 1-3 dots. Yet it should be a valid `xs:ID`, but `ID` s need to be valid NCNames, so they can't begin with a number! Either I'm mistaken or there is a "bug" in your homework.
jasso
@jasso: thanks for corrections. I can't fix code in my answer since I don't know what does @kchau exactly want - sequence, group, choice. However fixed my answer to mention your comments.
Shcheklein
\d doesn't select the decimal point for me in this example: http://regexr.com?2s5dv, but I think you've pointed out what I did wrong. I want to keep the anonymous type, but I defined it wrong. And yeah, I also left out the grouping for the elements. And, yes, I modified my ID pattern to include a two letter prefix, so that a valid ID would look like 'CS605.444'. Thanks for the help guys.
kchau