问题
I have created XML Schema for an XML document that describes functionality of components of a complex modular system. Within that XML document, I want to include XML Schema which will be read and parsed to allow for configuration.
meta-schema.xsd
(heavily edited for brevity):
<xs:schema targetNamespace="urn:project"
xmlns="urn:project"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Schema" type="SchemaType"/>
<xs:complexType name="SchemaType">
<xs:sequence>
<xs:element type="ConfigurationType" name="Configuration"/>
<xs:any maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ConfigurationType">
<xs:sequence>
<xs:element ref="xs:schema"/> <!-- Does not work -->
</xs:sequence>
<xs:anyAttribute/>
</xs:complexType>
</xs:schema>
The desired XML (written by folks developing modules):
<Schema xmlns="urn:project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:project meta-schema.xsd">
<!-- Snip -->
<Configuration>
<schema>
<element name="enable" type="boolean">
<annotation>
<appinfo>Usage:</appinfo>
<documentation xml:lang="en">
Enable functionality
</documentation>
</annotation>
</element>
</schema>
</Configuration>
</Schema>
Is this possible to express in XSD? If so, how?
Edit:
Based on kjhughes's comment, this is not possible. My solution was to use an any
element with an ##other
namespace, with a comment:
<xs:complexType name="ConfigurationType">
<xs:choice>
<xs:element name="hex" type="xs:hexBinary"/>
<xs:element name="base64" type="xs:base64Binary"/>
<!-- If configuration isn't binary, modules should create an XML Schema of the configuration options in order to
facilitate future tooling, when feasible. -->
<xs:any namespace="##other" processContents="lax"/>
</xs:choice>
<xs:anyAttribute/>
</xs:complexType>
Which enables the following XML:
<Schema xmlns="urn:project"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:project meta-schema.xsd">
<!-- Snip -->
<Configuration>
<xs:schema targetNamespace="urn:project"
xmlns="urn:project"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="enable" type="xs:boolean">
<xs:annotation>
<xs:appinfo>Usage:</xs:appinfo>
<xs:documentation xml:lang="en">
Enable left radial pulse functionality
</xs:documentation>
</xs:annotation>
</xs:element>
</xs:schema>
</Configuration>
</Schema>
回答1:
The link between an XML document instance and its associated XSD is established via xsi:schemaLocation
or xsi:noNamespaceSchemaLocation
.
- See How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?
It would be highly unconventional to attempt to physically include the XSD within the XSD. (There is no XSD equivalent to XML DTD's internal subset.)
- See Is it possible to use XML Schemas internally, just like DTDs?
You can, of course, always add an attribute or element of type xs:anyURI
to express such a connection, but that association will be opaque to XML/XSD semantics. You could also use external entities to incorporate any file, including an XSD, but, again, this is a file-level, not an XML/XSD-level mechanism.
来源:https://stackoverflow.com/questions/55195445/xsd-for-xml-containing-xsd