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

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

问题描述

elementFormDefault 有什么作用,什么时候使用?

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

<块引用>

qualified - 元素和属性位于目标名称空间中架构

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

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

这是我现在正在使用的文件,为什么当我将 targetNamespace 声明为与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="合格"><元素名称="分配"><复杂类型><序列><元素名称=分配"类型=目标:类型分配"minOccurs="1" maxOccurs="unbounded"/></序列></complexType></元素><complexType name="TypeAssignments"><序列><元素名称=分配"类型=目标:分配信息"minOccurs="0" maxOccurs="unbounded"/></序列></complexType><complexType name="assignmentInfo"><序列><元素名称=名称"类型=字符串"/><元素名称=页面"类型=目标:TypePage"/><元素名称=文件"类型=目标:类型文件"minOccurs="0" maxOccurs="unbounded"/></序列><属性名="id" type="string" use="required"/></complexType><simpleType name="TypePage"><限制基数=整数"><minInclusive value="50"/><maxInclusive value="498"/></限制></simpleType><simpleType 名称="类型文件"><限制基数=字符串"><枚举值=".xml"/><枚举值=".dtd"/><枚举值=".xsd"/></限制></simpleType></模式>

解决方案

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

这是规范的相关部分:

<块引用>

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

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

例如:如果 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" 来覆盖.

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天全站免登陆