Xsd 如何限制包含的架构
每个人 我有一个xsd模式(cft.xsd),它描述了一个非常通用的顶级结构:Xsd 如何限制包含的架构,xsd,xml-namespaces,Xsd,Xml Namespaces,每个人 我有一个xsd模式(cft.xsd),它描述了一个非常通用的顶级结构: <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cft="http://www.foo.com/xsd/cft" targetNamespace="http://www.foo.com/xsd/cft" elementFormDefault="qualified">
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cft="http://www.foo.com/xsd/cft" targetNamespace="http://www.foo.com/xsd/cft" elementFormDefault="qualified">
<xsd:complexType name="dirType">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="dir" type="cft:dirType"/>
<xsd:element name="txt">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="Name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9a-zA-Z_]+\.txt"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xml">
<xsd:complexType>
<xsd:sequence maxOccurs="1" minOccurs="1">
<xsd:any/>
</xsd:sequence>
<xsd:attribute name="Name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9a-zA-Z_]+\.xml"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
<xsd:element name="root" type="cft:dirType"/>
</xsd:schema>
以及第二个模式(dictionary.xsd),它将描述节点中需要的内容:
所以文件应该是这样的:
<cft:root xmlns:cft="http://www.foo.com/xsd/cft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.foo.com/xsd/cft cft.xsd">
<cft:xml Name="dictionary.xml">
<root xmlns="http://www.foo.com/xsd/dictionary" Version="17.0"/>
</cft:xml>
</cft:root>
我的问题是:
1.如何在dictionary.xsd中指定在那里定义的元素只能嵌套在元素中
-这是元素的子元素
-其名称属性为“dictionary.xml”
2.如何将cft.xsd中的元素限制为只有一个元素的名称来自与cft不同的名称空间
提前谢谢 首先,您的dictionary.xsd应该如下所示(修复:
complexType
而不是simpleType
作为根元素的类型)
然后,您可以使用自定义cft.xsd来验证XML:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://www.foo.com/xsd/cft" xmlns:dict="http://www.foo.com/xsd/dictionary" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.foo.com/xsd/cft" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:redefine schemaLocation="cft.xsd">
<xsd:complexType name="atype">
<xsd:complexContent>
<xsd:restriction base="atype">
<xsd:sequence>
<xsd:element ref="dict:root"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
<xsd:import schemaLocation="dictionary.xsd" namespace="http://www.foo.com/xsd/dictionary"/>
</xsd:schema>
作为最后一项建议,我建议不要使用xml
这样的名称,因为根据:以字符串“xml”开头的名称,或与任何匹配的字符串(('X'|'X')('M'|'M')('L'|'L'))的名称在本规范或本规范的未来版本中保留用于标准化。我见过一些产品简单地拒绝使用XML
作为标记名的XML文档,因此出于互操作性的原因,最好避免使用它。首先,您的字典.xsd应该如下所示(修复:complexType
而不是simpleType
作为根元素的类型).
然后,您可以使用自定义cft.xsd来验证XML:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://www.foo.com/xsd/cft" xmlns:dict="http://www.foo.com/xsd/dictionary" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.foo.com/xsd/cft" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:redefine schemaLocation="cft.xsd">
<xsd:complexType name="atype">
<xsd:complexContent>
<xsd:restriction base="atype">
<xsd:sequence>
<xsd:element ref="dict:root"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
<xsd:import schemaLocation="dictionary.xsd" namespace="http://www.foo.com/xsd/dictionary"/>
</xsd:schema>
作为最后一项建议,我建议不要使用xml
这样的名称,因为根据:以字符串“xml”开头的名称,或与任何匹配的字符串(('X'|'X')('M'|'M')('L'|'L'))的名称在本规范或本规范的未来版本中保留用于标准化。我见过一些产品简单地拒绝使用XML
作为标记名的XML文档,因此出于互操作性的原因,最好避免使用它
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cft="http://www.foo.com/xsd/cft" xmlns="http://www.foo.com/xsd/cft" targetNamespace="http://www.foo.com/xsd/cft" elementFormDefault="qualified">
<xsd:complexType name="dirType">
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="dir" type="cft:dirType"/>
<xsd:element name="txt">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="Name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9a-zA-Z_]+\.txt"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="xml" type="atype"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="atype">
<xsd:sequence>
<xsd:any/>
</xsd:sequence>
<xsd:attribute name="Name" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9a-zA-Z_]+\.xml"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
<xsd:element name="root" type="cft:dirType"/>
</xsd:schema>
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://www.foo.com/xsd/cft" xmlns:dict="http://www.foo.com/xsd/dictionary" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.foo.com/xsd/cft" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:redefine schemaLocation="cft.xsd">
<xsd:complexType name="atype">
<xsd:complexContent>
<xsd:restriction base="atype">
<xsd:sequence>
<xsd:element ref="dict:root"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:redefine>
<xsd:import schemaLocation="dictionary.xsd" namespace="http://www.foo.com/xsd/dictionary"/>
</xsd:schema>