2016-12-15 150 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對命名空間的影響之前,我建議您理解規則應用於單個模式。

而且它會幫助,如果你在未來提供更完整的樣本。