2016-12-15 154 views
2

如果我有以下XSD片段(使用MyRootNs但无所谓)命名空间中的XML

<xs:complexType name="SomeType"> 
    <xs:sequence> 
     <xs:element name="SomeElement" type="ns1:SomeType" /> 
     ... 

这是否导致

<SomeType xmlns="MyRootNs" xmlns:ns1="MySecondNS"> 
    <SomeElement> 
     ... 
    </SomeElement> 
</SomeType> 

<SomeType xmlns="MyRootNs" xmlns:ns1="MySecondNS"> 
    <ns1:SomeElement> 
     ... 
    </ns1:SomeElement> 
</SomeType> 

我发现无论是在

XSD with elements from other namespace

https://www.codeproject.com/articles/18455/xsd-tutorial-part-of-namespaces

哪一个是正确的?

回答

0

它不会“导致”两种。模式中的SomeType是类型的名称,而不是元素声明的名称。它当然也可以是元素声明的名称,但我们不知道它在哪个名称空间中。我们也不能看到MySecondNS在模式中的何处(如果有的话)。

0

你没有真正提供足够的信息。正如Michael指出的那样,您不能从complexType定义中创建XML元素。

但无论哪种情况下是有效的给予正确的模式。

我还要指出的是,这个样本中的xmlns:NS1 =“MySecondNS”语句什么都不做,它只是声明命名空间。一旦宣布它不被使用。

<SomeType xmlns="MyRootNs" xmlns:ns1="MySecondNS"> 
    <SomeElement> 
     ... 
    </SomeElement> 
</SomeType> 

如果你的模式是这样的

<?xml version="1.0" encoding="utf-8" ?> 
<!--Created with Liquid XML 2017 Developer Bundle Edition (Trial) 15.0.0.6978 (https://www.liquid-technologies.com)--> 
<xs:schema elementFormDefault="qualified" targetNamespace="http://MyNamespce1" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:q1="http://MyNamespce1"> 
    <xs:complexType name="SomeType"> 
     <xs:sequence> 
      <xs:element name="SomeElement" type="q1:SomeType" minOccurs="0" /> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:element name="MyRoot" type="q1:SomeType" /> 
</xs:schema> 

enter image description here

然后有效的XML是这样的

<?xml version="1.0" encoding="utf-8"?> 
<!-- Created with Liquid XML 2017 Developer Bundle Edition (Trial) 15.0.0.6978 (https://www.liquid-technologies.com) --> 
<MyRoot xmlns="http://MyNamespce1" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://MyNamespce1 Schema.xsd"> 
    <SomeElement> 
     <SomeElement> 
      <SomeElement></SomeElement> 
     </SomeElement> 
    </SomeElement> 
</MyRoot> 

<?xml version="1.0" encoding="utf-8"?> 
<!-- Created with Liquid XML 2017 Developer Bundle Edition (Trial) 15.0.0.6978 (https://www.liquid-technologies.com) --> 
<ns:MyRoot xmlns:ns="http://MyNamespce1" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://MyNamespce1 Schema.xsd"> 
    <ns:SomeElement> 
     <ns:SomeElement> 
      <ns:SomeElement></ns:SomeElement> 
     </ns:SomeElement> 
    </ns:SomeElement> 
</ns:MyRoot> 

的命名空间的规则是一个单一的文件中相当简单,但与被inlucded或导入多个模式文件打交道时得到相当复杂。在尝试理解import/include对命名空间的影响之前,我建议您理解规则应用于单个模式。

而且它会帮助,如果你在未来提供更完整的样本。