【问题标题】:specified schema XSD for XML nested nodes为 XML 嵌套节点指定模式 XSD
【发布时间】:2014-01-31 06:04:41
【问题描述】:

我正在尝试为 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>

【问题讨论】:

  • 即使你很尴尬,你也应该提供你到目前为止所拥有的。 StackOverflow 不是免费的 XSD 编写服务。
  • @JLRishe JL,我添加了我的代码,您是否碰巧看到任何语法或逻辑问题?谢谢。
  • 您的 XSD 目前因三个原因无效:(1) 在 xmlns:employeelist 之间有一个空格。 (2) minInclusivemaxInclusive 直接在 simpleType 元素中,但它们需要包含在 restriction 元素中。 (3) 在您的最后一个 complexType 中,您使用前缀 employeeList(大写 L),但您在顶部声明的命名空间前缀是 employeelist(小写 L)。
  • @JLRishe,我已经根据 W3C 验证器修复了所有假定的问题,预计第 4 行的最后一条错误消息:Invalid content was found starting with element 'employee'. One of '{name}' is expected. 知道这可能是什么吗?再次感谢!

标签: xml xslt xsd


【解决方案1】:

您已将“payroll”的类型定义为“singleEmployeeType”,因此它需要在&lt;payroll&gt; 元素中直接包含单个员工的元素(namespouse 等)。这与您的输入 XML 不匹配,并且看起来不像您想要的那样。

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

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

在您的 XSD 中,您将 employeeType 定义为 1 个或多个 &lt;employee&gt; 元素。我建议您修复 XSD 中类型的命名,使其与它是否只接受一种或多种事物保持一致。无论你喜欢什么约定都可以,只要它是一致的。只要您始终使用“复数”或“添加列表”约定,您就可以将 employeeType 重命名为 employeesTypeemployeeListType

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    相关资源
    最近更新 更多