Specify namespace for an element in XSD

跟風遠走 提交于 2019-12-11 08:11:48

问题


I've got an XML for which I need to generate an XSD. My XML goes as follows:

Instance:

    <mes:GetInboundResponseGetInboundSMS 
          xmlns:mes="http://abcd.com">
         <response>
            <messages>
               <item>
                  <date>15/04/2014 00:00:00</date>
               </item>
               <item>
                  <date>01/07/2014 10:01:32</date>
               </item>
            </messages>
         </response>
    </mes:GetInboundResponseGetInboundSMS>

Please note that only the outermost element GetInboundResponseGetInboundSMS belongs to a namespace http://abcd.com - the rest of the elements don't. How do I specify this in the XSD?

I've tried with following XSD but that gives me error:

XSD:

<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="GetInboundResponseGetInboundSMS">
    <xs:complexType>
      <xs:sequence>

<xs:element name="response">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="messages">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="item" 
                          maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="date"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

But when I tried to validate the instance with the XSD using an online validator, I ended up with this error:

Not valid.
Error - Line 1, 95: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 95; cvc-elt.1: Cannot find the declaration of element 'mes:GetInboundResponseGetInboundSMS'.

Which is logical, as I did not provide the namespace in the XSD. Please help me understanding how do I provide this namespace ONLY FOR THE OUTERMOST ELEMENT in my XSD.


回答1:


You've got two things that aren't quite what you want.

  • You want the GetInboundResponseGetInboundSMS element to be in the namespace http://abcd.com.

    So add targetNamespace="http://abcd.com" to your schema element.

  • You want the children of that element (which are all declared as local to the anonymous complex type of the GetInboundResponseGetInboundSMS element) to be unqualified.

    So change elementFormDefault="qualified" on the schema element to elementFormDefault="unqualified".

The start-tag for your schema document should look something like this:

<xs:schema targetNamespace="http://abcd.com"
           attributeFormDefault="unqualified" 
           elementFormDefault="unqualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">


来源:https://stackoverflow.com/questions/25398863/specify-namespace-for-an-element-in-xsd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!