对于看起来像这样的XML文档,
<Products>
<productTypes>
<productType name="BigOranges">
<product>
<name>BigOrange1</name>
<quatity>25</quatity>
</product>
<product>
<name>BigOrange2</name>
<quatity>55</quatity>
</product>
</productType>
<productType name="BigApples">
<product>
<name>BigApples1</name>
<quatity>25</quatity>
</product>
<product>
<name>BigApples2</name>
<quatity>55</quatity>
</product>
</productType>
</productTypes>
</Products>
我尝试自动生成XSD文件以查看示例,这就是生成的内容。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="T_quatity">
<xs:restriction base="xs:byte">
<xs:enumeration value="25"/>
<xs:enumeration value="55"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="T_name">
<xs:restriction base="xs:string">
<xs:enumeration value="BigApples1"/>
<xs:enumeration value="BigApples2"/>
<xs:enumeration value="BigOrange1"/>
<xs:enumeration value="BigOrange2"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AT_1">
<xs:restriction base="xs:string">
<xs:enumeration value="BigApples"/>
<xs:enumeration value="BigOranges"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="T_productTypes">
<xs:sequence>
<xs:element ref="productType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="T_productType">
<xs:sequence>
<xs:element ref="product" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="name" use="required"/>
</xs:complexType>
<xs:complexType name="T_product">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="quatity"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="T_Products">
<xs:sequence>
<xs:element ref="productTypes"/>
</xs:sequence>
</xs:complexType>
<xs:attribute name="name" type="AT_1"/>
<xs:element name="quatity" type="T_quatity"/>
<xs:element name="productTypes" type="T_productTypes"/>
<xs:element name="productType" type="T_productType"/>
<xs:element name="product" type="T_product"/>
<xs:element name="name" type="T_name"/>
<xs:element name="Products" type="T_Products"/>
</xs:schema>
看看上面的内容,我可以看到正在发生的事情是它们的类型是定义的,然后在定义元素时使用它们。我不明白的是,元素是一个接一个地定义的,不遵循XML消息的结构。
如果此架构将用于验证XML文档,那么将如何验证XML文件的结构?例如,使用上面的模式,它如何知道productTypes元素是productType标记的内部标记?
答案 0 :(得分:2)
你对这个问题的说明有点不对...它应该它是如何知道productTypes元素是 productType 产品的内部标记标记
我认为令人困惑的是全局元素在XSD文件中显示的顺序,它与生成的XSD的创作风格有关,风格通常被称为“伊甸园”。
应该注意的是,这些全局定义在XSD中的显示方式与兼容的XML实例之间没有关系。
例如,如果您熟悉UML类图,那么无关紧要就像在可视化UML类图中类的位置是在现实世界中实际使用类一样。
作为一个侧边栏,我现在可以看到,从你的困惑中,在伊甸园中学习XSD;)如果试图遵循XML结构,可能会成为一个糟糕的起点。
下面是另一个生成的XSD,它适用于相同的XML;我认为你可能会发现这更直观(顺便说一句,这种创作风格因其深层嵌套结构而被称为“俄罗斯娃娃”)。
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Products">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="productTypes">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="productType">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="product">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="quatity" type="xsd:unsignedByte" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我认为上述内容也强调并澄清了@Romil对你案件的回答(他指的是T_Products)。
关于验证的工作原理,解释并不容易。您可以将其视为状态机,其中状态之间的转换由您在XSD中的约束(最小/最大发生,序列与选择与所有)相关联。 XML阅读器遍历XML,并试图从一个州转移到另一个州;当它到达一个没有出路的地方时,它可能是一个错误,或成功的验证,取决于......
答案 1 :(得分:1)
xs:complexType用于显示哪个对象将嵌套在此父对象中。 xs:sequence显示父对象中子对象的顺序。
xs:simpleType表示它是原子对象。