program story

XSD에서 elementFormDefault는 무엇을합니까?

inputbox 2020. 10. 7. 07:37
반응형

XSD에서 elementFormDefault는 무엇을합니까?


무엇을 elementFormDefault하고 언제 사용해야합니까?

그래서 elementFormDefault값에 대한 몇 가지 정의를 찾았습니다 .

규정 됨 -요소 및 속성이 스키마의 targetNamespace에 있습니다.

규정되지 않음 -요소 및 속성에 네임 스페이스가 없습니다.

그래서 그 정의에서 스키마가 정규화로 설정되어 있다면 왜 네임 스페이스를 타입에 접두사로 붙여야합니까? 그리고 그 문제에 대해 자격을 갖추지 못한 시나리오는 무엇입니까? 인터넷 검색을 시도했지만 이해하기 매우 어려운 W3C 페이지 몇 개뿐이었습니다.

이것은 내가 지금 작업하고있는 파일입니다 target:TypeAssignments. 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="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는 스키마에있는 유형의 네임 스페이스와 관련이 없으며 스키마를 준수하는 XML 문서의 요소 네임 스페이스에 관한 것입니다.

사양의 관련 섹션은 다음과 같습니다.

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

즉, 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"를 명시하면 요소 선언에 form = "unqualified"를 넣어 재정의하지 않는 한 targetNamespace가 각 요소에 적용됩니다.


요소에서 AuthorType사용 하는 다음 ComplexType을 고려하십시오.author

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

작성자의 이름 속성은 네임 스페이스 (unqualified)를 지정하지 않고 허용됩니다. 의 일부인 모든 요소는 <xsd:complexType>complexType에 대한 로컬로 간주됩니다.

만약 elementFormDefault="qualified"

그런 다음 인스턴스에는 정규화 된 로컬 요소가 있어야합니다.

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

자세한 내용은 링크를 참조하십시오


오래된 자주 묻는 질문에 대한 새롭고 자세한 답변 및 설명 ...

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.

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.

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


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

참고URL : https://stackoverflow.com/questions/1463138/what-does-elementformdefault-do-in-xsd

반응형