Xml 如何使用属性限制complexType中的元素内容?
我试图在XSD的帮助下验证XML 以下是我的XML示例:Xml 如何使用属性限制complexType中的元素内容?,xml,xsd,xsd-validation,xml-validation,Xml,Xsd,Xsd Validation,Xml Validation,我试图在XSD的帮助下验证XML 以下是我的XML示例: <Cells xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "ImportCellsXSD.xsd"> <Cell spread="2" row="128" column="51">p</Cell> <Cell spread="0" ro
<Cells
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation= "ImportCellsXSD.xsd">
<Cell spread="2" row="128" column="51">p</Cell>
<Cell spread="0" row="10" column="1">ea</Cell>
<Cell spread="0" row="8" column="2">d</Cell>
</Cells>
P
ea
D
这是我的XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Cells">
<xs:complexType>
<xs:sequence>
<xs:element name="Cell" type="TCell" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="TCell">
<xs:sequence>
<xs:element name="Cell" type="TCellValidation" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="spread" type="TSpread" use="required"/>
<xs:attribute name="row" type="TRow" use="required"/>
<xs:attribute name="column" type="TColumn" use="required"/>
</xs:complexType>
<xs:simpleType name="TCellValidation">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
TSpread
、TColumn
和TRow
只是对minInclusive
和maxInclusive
当我尝试验证xml时,在尝试读取“p”、“ea”和“d”时会出现此错误
cvc复杂类型.2.3:元素“Cell”不能有字符[子元素],
因为类型的内容类型仅为元素。[6]
cvc复合类型。2.4.b:元素“Cell”的内容不完整。
应为“{Cell}”中的一个。[6]
如何定义
单元格的内容
?(p、ea和d)您的XSD要求Cell
元素位于Cell
元素中——可能不是您想要的,也肯定不是您的XML所拥有的
以下是修复的XSD,用于消除上述问题并验证XML:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" attributeFormDefault="unqualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Cells">
<xs:complexType>
<xs:sequence>
<xs:element name="Cell" type="TCell" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="TCell">
<xs:simpleContent>
<xs:extension base="TCellValidation">
<xs:attribute name="spread" type="xs:string" use="required"/>
<xs:attribute name="row" type="xs:string" use="required"/>
<xs:attribute name="column" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="TCellValidation">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
(
TSpread
、TColumn
和TRow
未在您发布的XSD中定义,因此我将它们删除为xs:string
,但原则不变。)谢谢!它成功了,SimpleContent和扩展解决了我的问题