问题
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 toelementFormDefault="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