targetNamespace and xmlns without prefix, what is the difference?

后端 未结 6 1718
忘掉有多难
忘掉有多难 2020-11-30 17:46

In an xml schema document, if I have both the targetNamespace and the xmlns without a prefix.



        
6条回答
  •  广开言路
    2020-11-30 18:19

    Other answers are good here, so I will not repeat their explanations here. However, if anyone from Java background, find its simpler, here is the analogy I came up with -

    1. .xsd document is the artifact/.jar file
    2. xmlns is the

      package com.example
      

      statement, you declare at the top of your Java classes.

    Consider (for analogy), if you had one single package in your Java project, and all the classes are declared and defined within a single outer class. For eg,

        package com.furniture.models
    
        public class FurnitureShop {
    
             int noOfTables;
             int noOfChairs;
             int noOfBeds;
             List tables;
             List chairs;
             List 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;
                 ...
             }
        }
    
    
    

    This is how different elements are grouped in a single .xsd file, for a new schema.

    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 -

        
           com.furniture
           furniture-apis
           1.1.1
        
    

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

        
    

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

    提交回复
    热议问题