Programing

접두사가없는 targetNamespace 및 xmlns, 차이점은 무엇입니까?

lottogame 2020. 10. 30. 07:40
반응형

접두사가없는 targetNamespace 및 xmlns, 차이점은 무엇입니까?


xml 스키마 문서에서 접두사없이 targetNamespace와 xmlns가 모두있는 경우 .

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://example.com/" xmlns="http://example.com/">

그들 사이의 정확한 차이점은 무엇입니까? 내 이해는 접두사가없는 xmlns가 있으면 접두사가없는 모든 요소가 해당 네임 스페이스를 가져오고 혼란스럽게도 targetNamespace에도 동일하게 적용된다는 것입니다.


targetNamespace 는 XML 스키마 "아티팩트"입니다. 목적 : 스키마 파일이 설명하는 특정 XML 네임 스페이스를 나타냅니다.

xmlns -XML 스키마가 XML 문서이기 때문에 XML 파일 자체에 대한 기본 XML 네임 스페이스를 정의 할 수 있습니다 (이것이 xmlns 속성이하는 일입니다). 그 의미는 여러 가지입니다 : 저작 및 구성. 예를 들어, 스키마에 정의 된 항목에 접두사를 사용할 필요가 없으며 나중에 동일한 파일의 다른 곳에서 참조됩니다 (예 : 속성 또는 요소의 유형으로 사용되는 전역 simpleType).

제 경험상 많은 XML 스키마 작성자는 이것을 "모범 사례"로 간주합니다. 따라서 올바른 방향으로 가고 있습니다.

XSD 측면에서 targetNamespace는 요소, 속성, 그룹 및 속성 그룹, 단순 및 복합 유형을 포함하는 스키마 구성 요소의 규정 된 이름의 네임 스페이스 부분을 규정합니다. XSD (요소 및 속성)에 정의 된 일부 규정 된 이름은 XML 인스턴스 문서에서 "직접"사용됩니다. for types와 같은 다른 항목 은 인스턴스 XML 문서 xsi : type 속성을 통해 참조 할 수 있습니다 . 나머지 (그룹, 속성 그룹)는 스키마 구성을 용이하게하기 위해 (참조를 통해) 있습니다.

나는 또한 (일반적으로) 사람들이 XSD를 두 가지 각도에서 디자인 할 것이라고 생각합니다.

  • 기존 XML과 일치합니다. 이 경우 XML에서 네임 스페이스를 사용하는 경우 사용 된 각 네임 스페이스에 대해 일치하는 targetNamespace 속성이있는 XSD 스키마 요소가 생성됩니다.

  • 순수한 모델링. 그런 다음 UML 패키지, 데이터베이스 스키마, Java 패키지 또는 .NET 네임 스페이스와 유사한 targetNamespace를 생각하면이 모든 것이이 경우에 의미합니다. 기본적으로 이름 충돌을 방지하는 메커니즘입니다. 그럼에도 불구하고 주제 영역 등에서 모델을 분할하는 메커니즘이기도합니다.


여전히 혼란스러운 사람들을 위해이 세 가지 xsd를 고려하십시오. 이들은 모두 하나의 글로벌 유형과이를 참조하는 하나의 글로벌 요소 정의를 정의합니다.

첫째, 위에 게시 된 것과 같은 xsd입니다. 스키마 네임 스페이스에는 접두사 'xsd'를 사용하고 targetNamespace에는 기본 네임 스페이스를 사용합니다.

<xsd:schema 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://example.com/" 
  xmlns="http://example.com/">

  <xsd:element name="aGlobalElement" type="aGlobalType"/>

  <xsd:simpleType name="aGlobalType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>   
</xsd:schema>  

이제 동일한 xsd이지만 대상 네임 스페이스에 네임 스페이스 접두사를 정의하고 사용합니다.

<xsd:schema 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://example.com/" 
  xmlns:tns="http://example.com/">

  <xsd:element name="aGlobalElement" type="tns:aGlobalType"/>

  <xsd:simpleType name="aGlobalType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType> 
</xsd:schema>  

... 그리고 마지막으로 XML 스키마 네임 스페이스에 'xsd'대신 기본 네임 스페이스를 사용하는 버전 :

<schema 
  xmlns="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://example.com/" 
  xmlns:tns="http://example.com/">

  <element name="aGlobalElement" type="tns:aGlobalType"/>

  <simpleType name="aGlobalType">
    <restriction base="string"/>
  </simpleType>
</schema>

대부분의 스키마 작성자는 첫 번째 또는 마지막을 선택합니다. 기본 네임 스페이스 기능을 사용할 수있는 경우이를 사용하는 것이 좋습니다.


xmlns

xmlns 속성은 설명 된 요소의 기본 네임 스페이스를 설정합니다. 따라서 기본 네임 스페이스는 설명 된 요소 내의 모든 요소에 적용되며, 명시 적으로 다른 네임 스페이스를 선언하지 않습니다.

기본 네임 스페이스는 WSDL 파일의 표준 값으로 설정됩니다. http://www.w3.org/ns/wsdl

targetNameSpace

이 속성에는 웹 서비스의 이름 공간이 포함됩니다. 이 네임 스페이스는 자유롭게 선택할 수 있지만 URI가 서비스의 WSDL을 가리켜 야한다는 규칙이 있습니다.

xmlns : tns

이 네임 스페이스는 targetNameSpace 속성과 동일한 URI로 설정되어야합니다. 이렇게하면이 네임 스페이스 접두사 (tns)를 통해 대상 네임 스페이스를 참조 할 수 있습니다.

출처 : http://tutorials.jenkov.com/wsdl/description.html


네임 스페이스는 범위와 같은 의미

targetNamespaceschema요소 의 속성 은 네임 스페이스 즉 XSD 파일의 패키지를 정의합니다. 관례 상 우리는 URI / URL을 사용하지만 어떤 문자열도 사용할 수 있습니다.

xmlns 속성은 현재 요소 범위에 대한 xmlns 속성 값에서 오는 요소 및 데이터 유형을 참조하는 데 사용됩니다.

예를 들면 :

  • xmlns:xsd="http://www.w3.org/2001/XMLSchema"는 접두사를 xsd사용하여 네임 스페이스에 접두사를 추가 해야 함을 의미합니다.xsd:
  • xmlns="http://www.w3.org/2001/XMLSchema" 접두사없이 기본값
  • xmlns : p = "http://www.example.com/People"에는 접두사 p가 있습니다. 이는 네임 스페이스에 접두사를 추가 해야 함을 의미합니다.p:

어디 xmlns:xsdxmlns:p의 QName이며, xmlns지역 이름입니다.

다음 이미지는 내 지식에 따라 Java 비유를 사용하여 XSD를 이해하는 데 도움이됩니다.

여기에 이미지 설명 입력


다른 답변은 여기에서 좋으므로 여기에서 설명을 반복하지 않겠습니다. 그러나 Java 배경을 가진 사람이 더 간단하다고 생각하면 여기에 내가 생각해 낸 비유가 있습니다.

  1. .xsd문서는 artifact / .jar파일입니다.
  2. xmlns 이다

    package com.example
    

    성명, 당신은 당신의 자바 클래스 의 맨 위에 선언합니다 .

Java 프로젝트에 하나의 단일 패키지가 있고 모든 클래스가 단일 외부 클래스 내에서 선언되고 정의 된 경우 (비유를 위해) 고려하십시오 . 예를 들어,

    package com.furniture.models

    public class FurnitureShop {

         int noOfTables;
         int noOfChairs;
         int noOfBeds;
         List<Table> tables;
         List<Chair> chairs;
         List<Bed> beds;

         // and now instead of declaring and defining a class for table/chair/bed in a 
         // separate file, you just add it here 
         public static class Table {
             int height;
             int width;
             int length;
             ...
         }

         public static class Chair {
             String color;
             ChairType chairType;
             ...
         }

         public static class Sofa {
             int price;
             String color;
             ...
         }
    }

.xsd새 스키마에 대해 여러 요소가 단일 파일에 그룹화되는 방식 입니다.

  1. targetNamespace is the name of the artifact you create. As you can find it out yourself, targetNamespace is used when creating a schema, in an .xsd file.

Once, the artifact(or .xsd file) is created, you'd use it in other projects as follows -

In a Java project, you'd import the library, using pom.xml (or build.gradle) file as follows -

    <dependency>
       <groupId>com.furniture</groupId>
       <artifactId>furniture-apis</artifactId>
       <version>1.1.1</version>
    </dependency>

In XML, you'd "import" the schema using

    <furniture xmlns="http://furniture.com"/>

=== APPENDIX ===

Clarification -

  1. xmlns is both used as a package statement, as well as the import statement in Java. In .xsd file, xmlns acts as the "package" statement, whereas in .xml files, it acts as the "import" statement.

After some thorough testing using xmllint I think I found the definite explanation here. Consider the below schema:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://yyyzzz.com"
xmlns:p="http://abced.com"
xmlns:q="http://pqr.com"
xmlns="http://yyyzzz.com">

<xsd:element name="recipe" type="recipeType" />

<xsd:complexType name="recipeType">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
        <xsd:attribute name="desc" type="xsd:string"  />
        <xsd:attribute name="archetype" type="xsd:string" />
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>
</xsd:schema>

The above schema validates to the below document:

<?xml version="1.0"?>

<recipe xmlns="http://yyyzzz.com">
    Deciphering the purpose of targetNamespace
</recipe>

The reason that works is because xmlns="http://yyyzzz.com" automatically binds to the element being defined by the schema too! That means, it binds also to the recipeType element.

Now, with same xml document but with slightly modified schema as below also validates and take a close look at the difference:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://yyyzzz.com"
xmlns="http://eigenfield.aparicio.com"
xmlns:EGboy="http://yyyzzz.com">

<xsd:element name="recipe" type="EGboy:recipeType" />

<xsd:complexType name="recipeType">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
        <xsd:attribute name="desc" type="xsd:string"  />
        <xsd:attribute name="archetype" type="xsd:string" />
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

</xsd:schema> 

다른 xmlns 가 누락 된 경우 무시하고 대신 type = "EGboy : recipeType"을 자세히 살펴보십시오 . 값이 다르기 때문에 더 이상 xmlns 에 의존 할 수 없으므로, recipeType 앞에 접두사 EGboy넣어야 합니다.

xml 문서는 EGboy 접두사 도 신경 쓰지 않습니다. 이 접두사는 스키마 가 많은 경우 적절한 xmlns 를 참조하기위한 것 입니다.

참고 URL : https://stackoverflow.com/questions/7190572/targetnamespace-and-xmlns-without-prefix-what-is-the-difference

반응형