elementFormDefault 在 XSD 中做什么?

发布于 2024-08-05 09:48:56 字数 2146 浏览 12 评论 0原文

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
schema

unqualified - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ゞ花落谁相伴 2024-08-12 09:48:56

ElementFormDefault 与模式中类型的名称空间无关,它与 XML 文档中符合模式的元素的名称空间有关。

这是规范的相关部分:

元素声明架构

组件属性{目标命名空间}
表示 如果存在形式并且其“实际价值”是合格的, 
                    或者如果形式不存在并且“实际价值” 
                     上的 elementFormDefault祖先有资格, 
                    那么 targetNamespace [属性] 的“实际值”
                    父的元素信息项,或 
                    如果没有则“缺席”,否则“缺席”。

这意味着,如果 elementFormDefault 为“qualified”,或者该元素在架构中显式声明为具有 form="qualified",则您在架构顶部声明的 targetNamespace 仅适用于符合架构的 XML 文档中的元素。

例如:如果 elementFormDefault 未限定 -

<element name="name" type="string" form="qualified"></element>
<element name="page" type="target:TypePage"></element>

将期望“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:

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".

For example: If elementFormDefault is unqualified -

<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.

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.

找个人就嫁了吧 2024-08-12 09:48:56

对旧的常见问题的新的详细答案和解释...

简短答案:如果您不添加 elementFormDefault ="qualified"xsd:schema,那么默认的 unqualified 值意味着本地声明的元素没有命名空间

关于 elementFormDefault 的作用存在很多困惑,但这可以通过一个简短的示例快速澄清...

XSD 的简化版本:

<?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">
  <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>

要点:

  • 赋值 元素是本地定义的。
  • 默认情况下,XSD 中本地定义的元素不在命名空间中。
    • 这是因为 elementFormDefault 的默认值为 unqualified
    • 这可以说是 XSD 创建者的设计错误。
    • 标准做法是始终使用 elementFormDefault="qualified"
      这样赋值就位于目标命名空间中
      期待。
  • 它是 xs:element 声明中很少使用的 form 属性,elementFormDefault 为其建立默认值。

看似有效的 XML

根据上述 XSD,此 XML 看起来应该是有效的:

<assignments xmlns="http://www.levijackson.net/web340/ns"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.levijackson.net/web340/ns try.xsd">
  <assignment id="a1">
    <name>John</name>
  </assignment>
</assignments>

注意:

  • 赋值 上的默认命名空间放置 赋值 以及所有它的后代在默认命名空间 (http://www.levijackson.net/web340/ns) 中。

令人困惑的验证错误

尽管上面的 XML 看起来有效,但它会产生以下令人困惑的验证错误:

[错误] try.xml:4:23: cvc-complex-type.2.4.a: 内容无效
发现以元素“赋值”开头。 “{赋值}”之一是
预计。

注释:

  • 您不会是第一个诅咒此诊断的开发人员,该诊断似乎表明内容无效,因为它期望找到 赋值 元素,但实际上它找到了一个赋值元素。(WTF
  • 这真正意味着什么:{}周围赋值意味着验证需要赋值在没有命名空间这里。不幸的是,当它说它找到了一个 assignment 元素时,它没有提到它在默认命名空间中找到了它,这与没有命名空间不同。

解决方案

  • 大多数情况下:将 elementFormDefault="qualified" 添加到 XSD 的 xsd:schema 元素。这意味着在 XSD 中本地声明时,有效的 XML 必须将元素放置在目标命名空间中;否则,有效的 XML 必须将本地声明的元素放置在任何名称空间中。
  • 极少数人:更改 XML 以符合 XSD
    要求赋值不在命名空间中。这是可以实现的,
    例如,通过将 xmlns="" 添加到 assignment 元素。

鸣谢:感谢Michael Kay对此答案提供的有用反馈。< /子>

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 unqualified 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:

<?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">
  <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 assignment element is locally defined.
  • Elements locally defined in XSD are in no namespace by default.
    • This is because the default value for elementFormDefault is unqualified.
    • This arguably is a design mistake by the creators of XSD.
    • Standard practice is to always use elementFormDefault="qualified"
      so that assignment is in the target namespace as one would
      expect.
  • It is a rarely used form attribute on xs:element declarations for which elementFormDefault establishes default values.

Seemingly Valid XML

This XML looks like it should be valid according to the above XSD:

<assignments xmlns="http://www.levijackson.net/web340/ns"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.levijackson.net/web340/ns try.xsd">
  <assignment id="a1">
    <name>John</name>
  </assignment>
</assignments>

Notice:

  • The default namespace on assignments places assignments 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:

[Error] try.xml:4:23: cvc-complex-type.2.4.a: Invalid content was
found starting with element 'assignment'. One of '{assignment}' is
expected.

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 assignment element but it actually found an assignment element. (WTF)
  • What this really means: The { and } around assignment means that validation was expecting assignment in no namespace here. Unfortunately, when it says that it found an assignment element, it doesn't mention that it found it in a default namespace which differs from no namespace.

Solution

  • Vast majority of the time: Add elementFormDefault="qualified" to the xsd: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.
  • Tiny minority of the time: Change the XML to comply with the XSD's
    requirement that assignment be in no namespace. This can be achieved,
    for example, by adding xmlns="" to the assignment element.

Credits: Thanks to Michael Kay for helpful feedback on this answer.

凝望流年 2024-08-12 09:48:56

考虑 author 元素使用以下 ComplexType AuthorType

<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"/>

如果 elementFormDefault="unqualified"

则以下 XML 实例有效,

<x:author xmlns:x="http://example.org/publishing">
   <name>Aaron Skonnard</name>
   <phone>(801)390-4552</phone>
</x:author>

允许作者的 name 属性,无需指定命名空间(非限定)。属于 一部分的任何元素都被视为复杂类型的本地元素。

如果 elementFormDefault="qualified"

则实例应具有合格的本地元素,

<x:author xmlns:x="http://example.org/publishing">
   <x:name>Aaron Skonnard</name>
   <x:phone>(801)390-4552</phone>
</x:author>

请参阅 链接了解更多详情

Consider the following ComplexType AuthorType used by author element

<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"

then following XML Instance is valid

<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.

if 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 this link for more details

醉酒的小男人 2024-08-12 09:48:56

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

纵性 2024-08-12 09:48:56

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.

辞慾 2024-08-12 09:48:56

我注意到,如果使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文