问题
I'm trying to wrap my head around namespaces and recursion in XSD and I feel lost.
Error: The QName value 'topic' does not resolve to a(n) element declaration
Most important part is divided with whitelines, just learning xsd but I need it for one of my assignments, so please, take it easy...
Code (I tried to follow this link: Recursive element in XML , unluckily no results):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name='xmap-content'>
<xs:complexType>
<xs:sequence>
<xs:element name='sheet'>
<xs:complexType>
<xs:sequence>
<xs:element name='topic'>
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='title' type='xs:string'/>
<xs:element name='children' minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name='topics'>
<xs:complexType>
<xs:sequence>
<xs:element ref='topic' minOccurs="0"
maxOccurs="unbounded"/>
</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:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Most important part is divided with whitelines, just learning xsd but I need it for one of my assignments, so please, take it easy...
回答1:
XSD recursive reference to an element (without namespaces)
In order to reference topic
recursively, it must be defined globally. Your attempt is failing because topic
is defined locally.
This will work:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name='xmap-content'>
<xs:complexType>
<xs:sequence>
<xs:element name='sheet'>
<xs:complexType>
<xs:sequence>
<xs:element ref='topic' minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='topic'>
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='title' type='xs:string'/>
<xs:element name='children' minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name='topics'>
<xs:complexType>
<xs:sequence>
<xs:element ref='topic' minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSD recursive reference to an element (with namespaces)
You mention namespaces in your question, but your included XSD makes no attempt to define namespaces. (Your problem as shown above did not have to do with namespaces per se either.) However, here is the above XSD adapted to work with namespaces in case you really did need them. Note that you have to define a namespace prefix to reference topic
element in the namespace given by xs:schema/targetNamespace
:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:e="http://www.example.com"
targetNamespace="http://www.example.com">
<xs:element name='xmap-content'>
<xs:complexType>
<xs:sequence>
<xs:element name='sheet'>
<xs:complexType>
<xs:sequence>
<xs:element ref='e:topic' minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='topic'>
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='title' type='xs:string'/>
<xs:element name='children' minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name='topics'>
<xs:complexType>
<xs:sequence>
<xs:element ref='e:topic' minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
来源:https://stackoverflow.com/questions/41818340/xml-xsd-recursion-and-namespaces