【问题标题】:XML Schema ComplexType Attribute RefXML Schema ComplexType 属性引用
【发布时间】:2015-02-11 07:08:11
【问题描述】:

我在为我的 XML 模式中的 complexType 分配属性时遇到了一些问题。我的架构的精简版如下:

<?xml version="1.0"?>

<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://xml-test.com"
    xmlns="http://xml-test.com"
    elementFormDefault="qualified">

    <!--++++++++++ ELEMENT DEFINTIONS ++++++++++-->

<xs:element name="DataRoot" type="RootDataType"/>

<!--++++++++++ TYPE DEFINTIONS ++++++++++-->

<!-- Document Root -->
<xs:complexType name="RootDataType">
    <xs:sequence minOccurs="1" maxOccurs="unbounded">
        <xs:element name="ValueSet" type="ValuesType"/>
    </xs:sequence>  

    <!-- Attributes -->
    <xs:attribute name="index" type="xs:integer" use="required"/>
</xs:complexType>

<!-- Value Set Type: A type representing a series of data values -->
<xs:complexType name="ValuesType">
    <xs:sequence minOccurs="1" maxOccurs="1">
        <xs:element name="FishCount" type="FishCountType"/>
    </xs:sequence>          

    <!-- Attributes -->
    <xs:attribute name="catcher" type="xs:string" use="required"/>
</xs:complexType>


<!-- Simple Fish Count type -->
<xs:simpleType name="SimpleFishCountType">
    <xs:restriction base="xs:decimal">
        <xs:totalDigits value="3"/>
        <xs:fractionDigits value="1"/>
    </xs:restriction>
</xs:simpleType>

<!-- Fish Count type -->
<xs:complexType name="FishCountType">
    <xs:simpleContent>
        <xs:extension base="SimpleFishCountType">
                <xs:attribute ref="interpolation"/>
        </xs:extension>                        
    </xs:simpleContent>                
</xs:complexType>

<!--++++++++++ ATTRIBUTE DEFINTIONS ++++++++++-->

<!-- Attribute to specify interpolation method -->
<xs:attribute name="interpolation">
    <xs:simpleType>
        <xs:restriction base="xs:integer">
        <xs:minInclusive value="0"/>
        <xs:maxInclusive value="1"/>
    </xs:restriction>
    </xs:simpleType>
</xs:attribute>

解释:我有一个“DataRoot”元素,它可以包含 1 个或多个“ValueSet”元素。我的 ValueSet 类型包含一个元素 FishCount,它是一个复杂类型 FishCountType。

FishCountType 由简单类型 SimpleFishCount(基于有限制的十进制数)和属性“插值”组成。

我正在使用的测试 XML 文件如下:

<?xml version="1.0"?>

<DataRoot index="1"
          xmlns="http://xml-test.com"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xml-test.com test-001.xsd">

    <ValueSet catcher="CaptainBirdsEye">
         <FishCount interpolation="0">12.3</FishCount>
    </ValueSet>
</DataRoot>

使用此验证器:http://www.utilities-online.info/xsdvalidation/#.UiC9UpK0KSo

我看到 XSD 和 XML 都是有效且格式正确的,但是在针对 XSD 验证 XML 时,我收到此错误:

Not valid.
Error - Line 9, 36: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 36; cvc-complex-type.3.2.2: Attribute 'interpolation' is not allowed to appear in element 'FishCount'.

似乎“FishCountType”中对“插值”属性的引用不正确,但我无法弄清楚。如果我在 FishCountType 类型中内联定义插值属性,并将其更改为 xs:integer,则一切正常。

我在这里做错了什么?我可以这样使用自定义属性吗?

感谢您的帮助,我的头几乎快秃了。

感谢@Michael Kay 解决

这就是我所做的:

    <xs:attributeGroup name="interpolation">
      <xs:attribute name="interpolation">
        <xs:simpleType>
          <xs:restriction base="xs:integer">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="1"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:attributeGroup>

【问题讨论】:

    标签: xml xsd schema complextype


    【解决方案1】:

    这是一个命名空间问题。您已将插值声明在架构的目标命名空间中。

    如果您想重用属性定义,通常解决此问题的方法是使用全局属性组中的局部声明来声明属性。作为本地声明,它(默认情况下)不在命名空间中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 2020-07-19
      • 2010-12-02
      • 2012-10-17
      • 2019-07-22
      相关资源
      最近更新 更多