具有固定属性的固定元素的XML模式?

问题描述:

什么是正确的XML模式1.0声明一个具有固定属性的固定元素的XML模式?

<notice xml:lang="en">Banana banana banana</notice> 

其中:

  1. 的xml:lang属性是强制性
  2. 值 “en” 是固定的,强制性的
  3. 通知的内容是简单的文字。
  4. 通知的内容是固定的(如上述),并强制?

我最好的(但错误)的努力是下面的代码片段:

<xs:element name="notice" use="required" fixed="Banana banana banana"> 
    <xs:complexType> 
    <xs:simpleContent> 
     <xs:extension> 
     <xs:attribute ref="xml:lang" use="required" fixed="en"/> 
     </xs:extension> 
    </xs:simpleContent> 
    </xs:complexType> 
</xs:element> 

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:import namespace="http://www.w3.org/XML/1998/namespace" /> 
    <xs:element name="notice" type="notice"/> 
    <xs:complexType name="notice"> 
     <xs:simpleContent> 
     <xs:extension base="CONTENT"> 
      <xs:attribute ref="xml:lang" use="required" fixed="en"/> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    <xs:simpleType name="CONTENT"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Banana banana banana"/> 
    </xs:restriction> 
    </xs:simpleType> 
</xs:schema> 
+0

感谢。只有一件事是错误的是,它不强制执行“en”作为xml:lang。我认为这可以通过向解决方案的xs:attribute节点添加fixed =“en”属性来实现。 – 2012-03-14 12:16:53

+0

是的,你是对的!很高兴它可以帮助。 :) – 2012-03-14 12:46:51