elementFormDefault 在 XSD 中做什么? [英] What does elementFormDefault do in XSD?

查看:23
本文介绍了elementFormDefault 在 XSD 中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

elementFormDefault 有什么作用,应该在什么时候使用?

所以我找到了一些 elementFormDefault 值的定义:

<块引用>

合格 - 元素和属性在 targetNamespace 中架构

不合格 - 元素和属性没有命名空间

因此,根据该定义,我认为如果将架构​​设置为限定的,那么为什么必须在类型前面加上命名空间?在哪些情况下您甚至会设置不合格?我尝试过谷歌搜索,但我得到的只是一些非常难以理解的 W3C 页面.

这是我现在正在处理的文件,为什么我需要将类型声明为 target:TypeAssignments 当我将 targetNamespace 声明为与xmlns:target?

<元素名称=赋值"><复杂类型><序列><元素名称=赋值"类型=目标:类型赋值"minOccurs="1" maxOccurs="unbounded"/></序列></complexType></元素><complexType name="TypeAssignments"><序列><元素名称=赋值"类型=目标:赋值信息"minOccurs="0" maxOccurs="unbounded"/></序列></complexType><complexType name="assignmentInfo"><序列><元素名称=名称"类型=字符串"/><element name="page" type="target:TypePage"/><元素名称=文件"类型=目标:TypeFile"minOccurs="0" maxOccurs="unbounded"/></序列><attribute name="id" type="string" use="required"/></complexType><simpleType name="TypePage"><restriction base="integer"><minInclusive value="50"/><maxInclusive value="498"/></限制></simpleType><simpleType name="TypeFile"><restriction base="string"><枚举值=".xml"/><枚举值=".dtd"/><枚举值=".xsd"/></限制></simpleType></架构>

解决方案

ElementFormDefault 与 schema 中类型的命名空间无关,它是关于 XML 文档中符合 schema 的元素的命名空间.

这是规范的相关部分:

<块引用>

元素声明架构组件属性 {目标命名空间}表示 如果形式存在并且其实际值是合格的,或者如果没有形式并且 上的 elementFormDefault祖宗有资格,然后是 targetNamespace [属性] 的实际值父<模式>元素信息项,或·缺席·如果没有,否则·缺席·.

这意味着您在架构顶部声明的 targetNamespace 仅适用于符合架构的 XML 文档中的元素,前提是 elementFormDefault 是合格的",或者该元素在架构中明确声明为具有 form=合格".

例如:如果 elementFormDefault 是不合格的 -

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

期望name"元素位于 targetNamespace 中,而page"元素位于空命名空间中.

为了避免您在每个元素声明上都添加 form="qualified",声明 elementFormDefault="qualified" 意味着 targetNamespace 适用于每个元素,除非通过在元素声明上添加 form="unqualified" 来覆盖.

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>

解决方案

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.

这篇关于elementFormDefault 在 XSD 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆