xml elementFormDefault 在 XSD 中做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1463138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does elementFormDefault do in XSD?
提问by Levi
What does elementFormDefaultdo, and when should it be used?
有什么作用elementFormDefault,什么时候应该使用?
So I found some definitions for elementFormDefaultvalues:
所以我找到了一些elementFormDefault值的定义:
qualified- elements and attributes are in the targetNamespace of the schema
unqualified- elements and attributes do not have a namespace
限定- 元素和属性在架构的 targetNamespace 中
unqualified- 元素和属性没有命名空间
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.
因此,根据该定义,我认为如果将模式设置为合格的,那么为什么必须在类型前面加上命名空间?在哪些情况下您甚至会设置不合格?我尝试过谷歌搜索,但我得到的只是一些非常难以理解的 W3C 页面。
This is the file I am working with right now, why do I need to declare the type as target:TypeAssignmentswhen I declare the targetNamespaceas the same one as xmlns:target?
这是我现在正在使用的文件,为什么我需要将类型target:TypeAssignments声明targetNamespace为与声明相同的类型xmlns:target?
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.leviHymanson.net/web340/ns"
targetNamespace="http://www.leviHymanson.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>
采纳答案by Alohci
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.
ElementFormDefault 与模式中类型的命名空间无关,它是关于 XML 文档中符合模式的元素的命名空间。
Here's the relevent section of the spec:
这是规范的相关部分:
Element Declaration Schema Component Property {target namespace} Representation If form is present and its ·actual value· is qualified, or if form is absent and the ·actual value· of elementFormDefault on the <schema> ancestor is qualified, then the ·actual value· of the targetNamespace [attribute] of the parent <schema> element information item, or ·absent· if there is none, otherwise ·absent·.
Element Declaration Schema Component Property {target namespace} Representation If form is present and its ·actual value· is qualified, or if form is absent and the ·actual value· of elementFormDefault on the <schema> ancestor is qualified, then the ·actual value· of the targetNamespace [attribute] of the parent <schema> element information item, or ·absent· if there is none, otherwise ·absent·.
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".
这意味着您在架构顶部声明的 targetNamespace 仅适用于符合架构的 XML 文档中的元素,如果 elementFormDefault 是“合格的”,或者该元素在架构中明确声明为具有 form="qualified" .
For example: If elementFormDefault is unqualified -
例如:如果 elementFormDefault 是不合格的 -
<element name="name" type="string" form="qualified"></element>
<element name="page" type="target:TypePage"></element>
will expect "name" elements to be in the targetNamespace and "page" elements to be in the null namespace.
将期望“name”元素在 targetNamespace 中,而“page”元素在空命名空间中。
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.
为了避免您必须在每个元素声明上放置 form="qualified",声明 elementFormDefault="qualified" 意味着 targetNamespace 适用于每个元素,除非通过在元素声明上放置 form="unqualified" 来覆盖。
回答by Girish
Consider the following ComplexType AuthorTypeused by authorelement
考虑元素AuthorType使用的以下 ComplexTypeauthor
<xsd:complexType name="AuthorType">
<!-- compositor goes here -->
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="phone" type="tns:Phone"/>
</xsd:sequence>
<xsd:attribute name="id" type="tns:AuthorId"/>
</xsd:complexType>
<xsd:element name="author" type="tns:AuthorType"/>
If elementFormDefault="unqualified"
如果 elementFormDefault="unqualified"
then following XML Instance is valid
那么下面的 XML 实例是有效的
<x:author xmlns:x="http://example.org/publishing">
<name>Aaron Skonnard</name>
<phone>(801)390-4552</phone>
</x:author>
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.
允许作者的姓名属性而不指定命名空间(非限定)。属于 的任何元素都<xsd:complexType>被认为是 complexType 的本地元素。
if elementFormDefault="qualified"
如果 elementFormDefault="qualified"
then the instance should have the local elements qualified
那么实例应该具有限定的本地元素
<x:author xmlns:x="http://example.org/publishing">
<x:name>Aaron Skonnard</name>
<x:phone>(801)390-4552</phone>
</x:author>
please refer thislink for more details
请参阅此链接了解更多详情
回答by kjhughes
New, detailed answer and explanation to an old, frequently asked question...
对一个旧的、常见的问题的新的、详细的答案和解释......
Short answer: If you don't add elementFormDefault="qualified"to xsd:schema, then the default unqualifiedvalue means that locally declared elements are in no namespace.
简短回答:如果您不添加elementFormDefault="qualified"to xsd:schema,则默认unqualified值意味着本地声明的元素不在 namespace 中。
There's a lot of confusion regarding what elementFormDefaultdoes, but this can be quickly clarified with a short example...
关于什么有很多困惑elementFormDefault,但这可以通过一个简短的例子迅速澄清......
Streamlined version of your XSD:
XSD 的简化版本:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.leviHymanson.net/web340/ns"
targetNamespace="http://www.leviHymanson.net/web340/ns">
<element name="assignments">
<complexType>
<sequence>
<element name="assignment" type="target:assignmentInfo"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"/>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
</schema>
Key points:
关键点:
- The
assignmentelement is locally defined. - Elements locally defined in XSD are in no namespace by default.
- This is because the default value for
elementFormDefaultisunqualified. - This arguably is a design mistake by the creators of XSD.
- Standard practice is to always use
elementFormDefault="qualified"so thatassignmentis in the target namespace as one would expect.
- This is because the default value for
- 该
assignment元素是本地定义的。 - 默认情况下,在 XSD 中本地定义的元素不在命名空间中。
- 这是因为默认值
elementFormDefault是unqualified。 - 这可以说是 XSD 的创建者的设计错误。
- 标准的做法是始终使用
elementFormDefault="qualified",以便assignment在目标命名空间正如人们所期望。
- 这是因为默认值
Seemingly Valid XML
看似有效的 XML
This XML looks like it should be valid according to the above XSD:
根据上面的 XSD,这个 XML 看起来应该是有效的:
<assignments xmlns="http://www.leviHymanson.net/web340/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.leviHymanson.net/web340/ns try.xsd">
<assignment id="a1">
<name>John</name>
</assignment>
</assignments>
Notice:
注意:
- The default namespace on
assignmentsplacesassignmentsand all of its descendents in the default namespace (http://www.leviHymanson.net/web340/ns).
- 在默认的命名空间
assignments的地方assignments在默认的命名空间,所有后代(http://www.leviHymanson.net/web340/ns)。
Perplexing Validation Error
令人困惑的验证错误
Despite looking valid, the above XML yields the following confusing validation error:
尽管看起来有效,但上面的 XML 会产生以下令人困惑的验证错误:
[Error] try.xml:4:23: cvc-complex-type.2.4.a: Invalid content was found starting with element 'assignment'. One of '{assignment}' is expected.
[错误] try.xml:4:23: cvc-complex-type.2.4.a: 发现以元素“赋值”开头的无效内容。'{assignment}' 之一是预期的。
Notes:
笔记:
- You would not be the first developer to curse this diagnostic that seems to say that the content is invalid because it expected to find an
assignmentelement but it actually found anassignmentelement.(WTF) - What this really means: The
{and}aroundassignmentmeans that validation was expectingassignmentin no namespacehere. Unfortunately, when it says that it found anassignmentelement, it doesn't mention that it found it in a default namespace which differs from no namespace.
- 您不会是第一个诅咒此诊断的开发人员,该诊断似乎说内容无效,因为它希望找到一个
assignment元素,但实际上找到了一个assignment元素。(跆拳道) - 这真正意味着什么: The
{和}aroundassignment意味着assignment在这里没有命名空间中需要验证。不幸的是,当它说它找到了一个assignment元素时,它没有提到它在一个不同于没有命名空间的默认命名空间中找到它。
Solution
解决方案
- Vast majority of the time:Add
elementFormDefault="qualified"to thexsd:schemaelement 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. - Tiny minority of the time:Change the XML to comply with the XSD's
requirement that
assignmentbe in no namespace. This can be achieved, for example, by addingxmlns=""to theassignmentelement.
- 绝大多数时间:添加
elementFormDefault="qualified"到xsd:schemaXSD的元素。这意味着当在 XSD 中本地声明时,有效的 XML 必须将元素放置在目标命名空间中;否则,有效的 XML 必须在没有命名空间的地方放置本地声明的元素。 - 极少数情况下:更改 XML 以符合 XSD 的
assignment无命名空间要求。例如,这可以通过添加xmlns=""到assignment元素来实现。
回答by stephan f
Important to note with elementFormDefault is that it applies to locallydefined 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.
重要的是要注意 elementFormDefault 是它适用于本地定义的元素,通常在 complexType 块内命名元素,而不是在架构的顶层定义的全局元素。使用 elementFormDefault="qualified",您可以使用架构的目标命名空间作为文档的默认命名空间,从 xml 文档中寻址架构中的本地元素。
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.
在实践中,使用 elementFormDefault="qualified" 能够在嵌套块中声明元素,否则您必须在顶层声明所有元素并使用 ref 属性在嵌套元素中的架构中引用它们,从而导致不那么紧凑的架构。
This bit in the XML Schema Primer talks about it: http://www.w3.org/TR/xmlschema-0/#NS
XML Schema Primer 中的这一点谈到了它:http: //www.w3.org/TR/xmlschema-0/#NS
回答by Feri
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).
elementFormDefault="qualified" 用于控制 XML 实例文档(.xml 文件)中名称空间的使用,而不是模式文档本身(.xsd 文件)中名称空间的使用。
By specifying elementFormDefault="qualified" we enforce namespace declaration to be used in documents validated with this schema.
通过指定 elementFormDefault="qualified",我们强制在使用此模式验证的文档中使用名称空间声明。
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.
通常的做法是指定这个值来声明元素应该是合格的而不是不合格的。但是,由于 attributeFormDefault="unqualified" 是默认值,因此如果不想限定名称空间,则不需要在模式文档中指定它。
回答by Neal
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
我注意到如果使用 elementFormDefault="qualified",XMLSpy(至少 2011 版)需要一个 targetNameSpace 定义。否则不会验证。并且也不会生成带有命名空间前缀的 xmls

