elementFormDefault 在 XSD 中做什么?
elementFormDefault
有什么作用,什么时候应该使用它?
所以我找到了 elementFormDefault
值的一些定义:
限定 - 元素和属性 位于的 targetNamespace 中 架构
不合格 - 元素和 属性没有命名空间
因此,根据该定义,我认为如果将模式设置为限定,那么为什么必须在类型前面加上命名空间?在什么情况下你甚至会认为他们不合格?我尝试用谷歌搜索,但得到的只是几个非常难以理解的 W3C 页面。
这是我现在正在使用的文件,为什么当我将 targetNamespace
声明为与 相同时,我需要将类型声明为
?target:TypeAssignments
xmlns:目标
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.levijackson.net/web340/ns"
targetNamespace="http://www.levijackson.net/web340/ns"
elementFormDefault="qualified">
<element name="assignments">
<complexType>
<sequence>
<element name="assignments" type="target:TypeAssignments"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="TypeAssignments">
<sequence>
<element name="assignment" type="target:assignmentInfo"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"/>
<element name="page" type="target:TypePage"/>
<element name="file" type="target:TypeFile"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
<simpleType name="TypePage">
<restriction base="integer">
<minInclusive value="50" />
<maxInclusive value="498" />
</restriction>
</simpleType>
<simpleType name="TypeFile">
<restriction base="string">
<enumeration value=".xml" />
<enumeration value=".dtd" />
<enumeration value=".xsd" />
</restriction>
</simpleType>
</schema>
What does elementFormDefault
do, and when should it be used?
So I found some definitions for elementFormDefault
values:
qualified - elements and attributes
are in the targetNamespace of the
schemaunqualified - elements and
attributes do not have a namespace
So from that definition I would think that if a schema is set to qualified then why must you prefix the type with the namespace? And what are the scenarios that you would even have one set to unqualified for that matter? I tried Googling, but all I got were a couple W3C pages that were extremely hard to understand.
This is the file I am working with right now, why do I need to declare the type as target:TypeAssignments
when I declare the targetNamespace
as the same one as xmlns:target
?
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.levijackson.net/web340/ns"
targetNamespace="http://www.levijackson.net/web340/ns"
elementFormDefault="qualified">
<element name="assignments">
<complexType>
<sequence>
<element name="assignments" type="target:TypeAssignments"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="TypeAssignments">
<sequence>
<element name="assignment" type="target:assignmentInfo"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"/>
<element name="page" type="target:TypePage"/>
<element name="file" type="target:TypeFile"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
<simpleType name="TypePage">
<restriction base="integer">
<minInclusive value="50" />
<maxInclusive value="498" />
</restriction>
</simpleType>
<simpleType name="TypeFile">
<restriction base="string">
<enumeration value=".xml" />
<enumeration value=".dtd" />
<enumeration value=".xsd" />
</restriction>
</simpleType>
</schema>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ElementFormDefault 与模式中类型的名称空间无关,它与 XML 文档中符合模式的元素的名称空间有关。
这是规范的相关部分:
这意味着,如果 elementFormDefault 为“qualified”,或者该元素在架构中显式声明为具有 form="qualified",则您在架构顶部声明的 targetNamespace 仅适用于符合架构的 XML 文档中的元素。
例如:如果 elementFormDefault 未限定 -
将期望“name”元素位于 targetNamespace 中,而“page”元素位于 null 命名空间中。
为了避免您必须在每个元素声明上放置 form="qualified",声明 elementFormDefault="qualified" 意味着 targetNamespace 适用于每个元素,除非通过在元素声明上放置 form="unqualified" 来覆盖。
ElementFormDefault has nothing to do with namespace of the types in the schema, it's about the namespaces of the elements in XML documents which comply with the schema.
Here's the relevent section of the spec:
What that means is that the targetNamespace you've declared at the top of the schema only applies to elements in the schema compliant XML document if either elementFormDefault is "qualified" or the element is declared explicitly in the schema as having form="qualified".
For example: If elementFormDefault is unqualified -
will expect "name" elements to be in the targetNamespace and "page" elements to be in the null namespace.
To save you having to put form="qualified" on every element declaration, stating elementFormDefault="qualified" means that the targetNamespace applies to each element unless overridden by putting form="unqualified" on the element declaration.
对旧的常见问题的新的详细答案和解释...
简短答案:如果您不添加
elementFormDefault ="qualified"
到xsd:schema
,那么默认的unqualified
值意味着本地声明的元素没有命名空间。关于
elementFormDefault
的作用存在很多困惑,但这可以通过一个简短的示例快速澄清...XSD 的简化版本:
要点:
赋值
元素是本地定义的。elementFormDefault
的默认值为unqualified
。elementFormDefault="qualified"
这样
赋值
就位于目标命名空间中期待。
xs:element
声明中很少使用的form
属性,elementFormDefault
为其建立默认值。看似有效的 XML
根据上述 XSD,此 XML 看起来应该是有效的:
注意:
赋值
上的默认命名空间放置赋值
以及所有它的后代在默认命名空间 (http://www.levijackson.net/web340/ns
) 中。令人困惑的验证错误
尽管上面的 XML 看起来有效,但它会产生以下令人困惑的验证错误:
注释:
赋值
元素,但实际上它找到了一个赋值
元素。(WTF){
和}
周围赋值
意味着验证需要赋值
在没有命名空间这里。不幸的是,当它说它找到了一个assignment
元素时,它没有提到它在默认命名空间中找到了它,这与没有命名空间不同。解决方案
elementFormDefault="qualified"
添加到 XSD 的xsd:schema
元素。这意味着在 XSD 中本地声明时,有效的 XML 必须将元素放置在目标命名空间中;否则,有效的 XML 必须将本地声明的元素放置在任何名称空间中。要求
赋值
不在命名空间中。这是可以实现的,例如,通过将
xmlns=""
添加到assignment
元素。鸣谢:感谢Michael Kay对此答案提供的有用反馈。< /子>
New, detailed answer and explanation to an old, frequently asked question...
Short answer: If you don't add
elementFormDefault="qualified"
toxsd:schema
, then the defaultunqualified
value means that locally declared elements are in no namespace.There's a lot of confusion regarding what
elementFormDefault
does, but this can be quickly clarified with a short example...Streamlined version of your XSD:
Key points:
assignment
element is locally defined.elementFormDefault
isunqualified
.elementFormDefault="qualified"
so that
assignment
is in the target namespace as one wouldexpect.
form
attribute onxs:element
declarations for whichelementFormDefault
establishes default values.Seemingly Valid XML
This XML looks like it should be valid according to the above XSD:
Notice:
assignments
placesassignments
and all of its descendents in the default namespace (http://www.levijackson.net/web340/ns
).Perplexing Validation Error
Despite looking valid, the above XML yields the following confusing validation error:
Notes:
assignment
element but it actually found anassignment
element. (WTF){
and}
aroundassignment
means that validation was expectingassignment
in no namespace here. Unfortunately, when it says that it found anassignment
element, it doesn't mention that it found it in a default namespace which differs from no namespace.Solution
elementFormDefault="qualified"
to thexsd:schema
element of the XSD. This means valid XML must place elements in the target namespace when locally declared in the XSD; otherwise, valid XML must place locally declared elements in no namespace.requirement that
assignment
be in no namespace. This can be achieved,for example, by adding
xmlns=""
to theassignment
element.Credits: Thanks to Michael Kay for helpful feedback on this answer.
考虑
author
元素使用以下 ComplexTypeAuthorType
如果
elementFormDefault="unqualified"
则以下 XML 实例有效,
允许作者的 name 属性,无需指定命名空间(非限定)。属于
一部分的任何元素都被视为复杂类型的本地元素。如果
elementFormDefault="qualified"
则实例应具有合格的本地元素,
请参阅 此链接了解更多详情
Consider the following ComplexType
AuthorType
used byauthor
elementIf
elementFormDefault="unqualified"
then following XML Instance is valid
the authors's name attribute is allowed without specifying the namespace(unqualified). Any elements which are a part of
<xsd:complexType>
are considered as local to complexType.if
elementFormDefault="qualified"
then the instance should have the local elements qualified
please refer this link for more details
elementFormDefault 需要注意的重要一点是,它适用于本地定义的元素,通常是在complexType 块内命名的元素,而不是在架构顶层定义的全局元素。通过 elementFormDefault="qualified",您可以使用架构的目标命名空间作为文档的默认命名空间,从 xml 文档中寻址架构中的本地元素。
在实践中,使用 elementFormDefault="qualified" 能够在嵌套块中声明元素,否则您必须在顶层声明所有元素,并使用 ref 属性在嵌套元素的架构中引用它们,从而导致更不紧凑的模式。
XML Schema Primer 中的这一点讨论了它:http://www.w3.org/TR/xmlschema- 0/#NS
Important to note with elementFormDefault is that it applies to locally defined elements, typically named elements inside a complexType block, as opposed to global elements defined on the top-level of the schema. With elementFormDefault="qualified" you can address local elements in the schema from within the xml document using the schema's target namespace as the document's default namespace.
In practice, use elementFormDefault="qualified" to be able to declare elements in nested blocks, otherwise you'll have to declare all elements on the top level and refer to them in the schema in nested elements using the ref attribute, resulting in a much less compact schema.
This bit in the XML Schema Primer talks about it: http://www.w3.org/TR/xmlschema-0/#NS
elementFormDefault="qualified" 用于控制 XML 实例文档(.xml 文件)中命名空间的使用,而不是架构文档本身(.xsd 文件)中命名空间的使用。
通过指定 elementFormDefault="qualified",我们强制在使用此模式验证的文档中使用命名空间声明。
通常的做法是指定此值来声明元素应该是限定的而不是不限定的。但是,由于 attributeFormDefault="unqualified" 是默认值,因此如果不想限定命名空间,则无需在架构文档中指定它。
elementFormDefault="qualified" is used to control the usage of namespaces in XML instance documents (.xml file), rather than namespaces in the schema document itself (.xsd file).
By specifying elementFormDefault="qualified" we enforce namespace declaration to be used in documents validated with this schema.
It is common practice to specify this value to declare that the elements should be qualified rather than unqualified. However, since attributeFormDefault="unqualified" is the default value, it doesn't need to be specified in the schema document, if one does not want to qualify the namespaces.
我注意到,如果使用 elementFormDefault="qualified",XMLSpy(至少 2011 版本)需要定义一个 targetNameSpace。否则不会验证。并且也不会生成带有命名空间前缀的 xml
I have noticed that XMLSpy(at least 2011 version)needs a targetNameSpace defined if elementFormDefault="qualified" is used. Otherwise won't validate. And also won't generate xmls with namespace prefixes