XML嵌套节点的指定模式XSD

时间:2014-01-31 06:04:41

标签: xml xslt xsd

我正在尝试为XML文件创建架构,但不断收到验证错误。我有过丰富的XML,XSLT和DTD经验,但当我被要求创建XSD文件时,我认为我可以解决这个问题。我被要求创建具有以下规范的模式:

    <payroll>
      <employee>+
        <name>
          <first>: string of upper/lowercase letters, spaces, or hyphens (-).
                   string must start with an uppercase letter
          <middle>?: upper case letter
          <last>: string of upper/lowercase letters, spaces, or hyphens (-).
                   string must start with an uppercase letter
        <spouse>? –- first, middle, last are the same type as for employee
          <first>
          <middle>?
          <last>
        <child>* -- first, middle, last are the same type as for employee
          <first>
          <middle>?
          <last>
        <tax-status> married | single | headOfHousehold | separated
        <ssn>: A nine digit number of the form ddd-dd-dddd (e.g, 865-57-2934)
            ssn attribute: name=type; values=(assigned | original); default="original"
        <salary>: A 9 digit number of the form ddddddd.dd with a minimum value of
                  0 and a maximum value of 2000000, inclusive.
        <date-of-birth>: A date type
        <manager> | <staff>
          <manager>
            attribute for manager: name=title; type=string; use=required
            <department>: string
            <yrsAtRank>: an inclusive integer between 0 and 50
          <staff>
            <skill>+: up to 5 skills, each being a string

这是XML文件:

    <?xml version = "1.0"?>

    <employeelist:payroll xmlns:employeelist = "urn:myURN:employeelist">
    <employee>
      <name>
        <first>Brad</first>
        <middle>T</middle>
        <last>Vander Zanden</last>
      </name>
      <spouse>
          <first>Yifan</first>
          <last>Tang</last>
      </spouse>
      <tax-status>married</tax-status>
      <ssn type="assigned">186-39-3069</ssn>
      <salary>110000.00</salary>
      <date-of-birth>1964-02-03</date-of-birth>
      <manager title="professor">
        <department>Computer Science</department>
        <yrsAtRank>8</yrsAtRank>
      </manager>
    </employee>

    <employee>
      <name>
        <first>Don</first>
        <last>Juan</last>
      </name>
      <child>
        <first>Mary</first>
        <middle>H</middle>
        <last>Lamb</last>
      </child>
      <child>
        <first>Fred</first>
        <last>Flintstone</last>
      </child>
      <tax-status>headOfHousehold</tax-status>
      <ssn>586-38-3969</ssn>
      <salary>1553.83</salary>
      <date-of-birth>1980-07-07</date-of-birth>
      <staff>
        <skill>Carpentry</skill>
        <skill>Welding</skill>
        <skill>Plumbing</skill>
      </staff>
    </employee>
    </employeelist:payroll>

而且..这里是XSD(下面)。我找不到问题。 W3C验证器只是说:“标记没有很好地形成。”任何线索或想法?提前谢谢!

<?xml version = "1.0"?>

<schema xmlns = "http://www.w3.org/2001/XMLSchema"
    xmlns:employeelist = "urn:csc420:employeelist"
    targetNamespace = "urn:csc420:employeelist">

    <complexType name="employeeType">
        <sequence>
            <element name="employee" type="employeelist:singleEmployeeType"
                minOccurs="1" maxOccurs="unbounded"/>
        </sequence>
    </complexType>

    <complexType name="nameType">
        <sequence>
            <element name="first" type="string"/>
            <element name="middle" type="string"/>
            <element name="last" type="string"/>
        </sequence>
    </complexType>

    <complexType name="spouseType">
        <sequence>
            <element name="first" type="string"/>
            <element name="middle" type="string"/>
            <element name="last" type="string"/>
        </sequence>
    </complexType>

    <simpleType name="tax-statusType">
            <restriction base = "string">
                <enumeration value = "married"/>
                <enumeration value = "single"/>
                <enumeration value = "headOfHousehold"/>
                <enumeration value = "separated"/>
            </restriction>
    </simpleType>

    <simpleType name="ssnType">
        <restriction base = "int">
            <pattern value = "[0-9]{3}\-[0-9]{2}\-[0-9]{4}"/>
        </restriction>
    </simpleType>

    <simpleType name="salaryType">
        <restriction base="decimal">
            <minInclusive value="0.00"/>
            <maxInclusive value="200000000.00"/>
        </restriction>
    </simpleType>

    <simpleType name="date-of-birthType">
        <restriction base="decimal">
            <minInclusive value = "0"/>
            <maxInclusive value = "10"/>
        </restriction>
    </simpleType>

    <simpleType name="skillType">
        <restriction base="decimal">
            <minInclusive value = "1"/>
            <maxInclusive value="5"/>
        </restriction>
    </simpleType>

    <complexType name="managerType">
        <sequence>
            <element name="department" type="string"/>
            <element name="yrsAtRank" type="int"/>
        </sequence>
        <attribute name="title" type="string"/>
    </complexType>

    <complexType name="staffType">
        <sequence>
            <element name="skill" type="employeelist:skillType"/>
        </sequence>
    </complexType>

    <complexType name="singleEmployeeType">
        <sequence>
            <element name="name" type="employeelist:nameType"/>
            <element name="spouse" type="employeelist:spouseType"/>
            <element name="tax-status" type="employeelist:tax-statusType"/>
            <element name="ssn" type="employeelist:ssnType"/>
            <element name="salary" type="employeelist:salaryType"/>
            <element name="date-of-birth" type="date"/>
            <element name="manager" type="employeelist:managerType"/>
            <element name="staff" type="employeelist:staffType"/>
        </sequence>
    </complexType>

    <element name="payroll" type="employeelist:singleEmployeeType"/>

</schema>

1 个答案:

答案 0 :(得分:1)

您已将“payroll”的类型定义为“singleEmployeeType”,因此它希望单个员工(namespouse等)的元素直接位于<payroll>内元件。这与您输入的XML不匹配,并且看起来不像您想要的那样。

只需将XSD中payroll元素的定义更改为:

即可
<element name="payroll" type="employeelist:employeeType"/>

在您的XSD中,您将employeeType定义为1个或多个<employee>元素。我建议你修改XSD中类型的命名,以确定它是否只接受一个或多个东西。无论您喜欢什么样的惯例都很好,只要它一致。只要您始终使用“复数”或“添加列表”惯例,就可以将employeeType重命名为employeesTypeemployeeListType