Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何使用属性限制complexType中的元素内容?_Xml_Xsd_Xsd Validation_Xml Validation - Fatal编程技术网

Xml 如何使用属性限制complexType中的元素内容?

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

我试图在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" 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和扩展解决了我的问题