定义允许来自混合命名空间的属性的模式

时间:2011-07-13 10:39:11

标签: xml xsd xml-namespaces

我正在努力开发一个允许来自混合命名空间的属性的模式。

这是xxx_schema2.xsd:

<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  attributeFormDefault="qualified"  
  targetNamespace="http://www.mrbouffant.com/schema2"
  xmlns:xxx="http://www.mrbouffant.com/schema2"> 

  <xs:attributeGroup name="schema2AttributeGroup">
    <xs:attribute name="schema2Attribute1 " type="xs:string"/>
    <xs:attribute name="schema2Attribute2 " type="xs:string"/>
  </xs:attributeGroup>

</xs:schema>

这是导入xxx_schema2的xxx_schema1.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified"
xmlns:xxx="http://www.mrbouffant.com/schema2">

  <xs:import namespace="http://www.mrbouffant.com/schema2" schemaLocation="xxx_schema2.xsd"/>

  <!-- ROOT ELEMENT -->
  <xs:element name="rootElement" type="rootElementType" />

  <!-- COMPLEX TYPES -->
  <xs:complexType name="rootElementType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attributeGroup ref="xxx:schema2AttributeGroup"/>  
            <xs:attribute name="schema1Attribute1" type="xs:string"/>
        </xs:extension>
     </xs:simpleContent>
  </xs:complexType>
</xs:schema>

以下是我想针对xxx_schema1.xsd验证的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
  <rootElement xmlns:xxx='http://www.mrbouffant.com/schema2/' 
schema1Attribute1="foo"
xxx:schema2Attribute1="bar"
xxx:schema2Attribute2="far" />

当Saxon-EE解析器尝试根据模式验证XML文档时,它生成的错误实际上是:

Engine name: Saxon-EE 9.3.0.5
Severity: error
Description: Attribute @xxx:schema2Attribute1 is not allowed on element <rootElement>
(it would be allowed in namespace http://www.mrbouffant.com/schema2)

Engine name: Saxon-EE 9.3.0.5
Severity: error
Description: Attribute @xxx:schema2Attribute2 is not allowed on element <rootElement>
(it would be allowed in namespace http://www.mrbouffant.com/schema2)

请问您能帮我理解我在模式定义或XML文档中做错了什么,这会阻止验证成功吗?谢谢。

1 个答案:

答案 0 :(得分:2)

实例XML中http://www.mrbouffant.com/schema2的名称空间声明有一个尾部斜杠,与Schema声明的名称空间不匹配。

删除尾部斜杠,它验证就好了:

<?xml version="1.0" encoding="UTF-8"?>
  <rootElement xmlns:xxx='http://www.mrbouffant.com/schema2' 
schema1Attribute1="foo"
xxx:schema2Attribute1="bar"
xxx:schema2Attribute2="far" />