我正在尝试学习将XML文件转换为XSD。我正在阅读和阅读互联网上的一些教程,但我没有发现何时必须在XSD中使用属性。如果我有这个XML文件:
?xml version="1.0" encoding="UTF-8"?>
<xmlns:f="http://www.w3schools.com/books"/>
<catalog>
<book id="book_1">
<author>Dave Winer</author>
<title>Learn XML</title>
<genre>IT Science</genre>
<publish_date>1999-03-03</publish_date>
<price>30</price>
<description>A insight in the XML world</description>
</book>
</catalog>
我试过写XSD文件。它看起来是否正确,还是需要一些属性?如果我何时以及何时必须使用它?
最好的问候朱莉
<xs:element name="catalog">
<xs:complexType>
<sequence>
<xs:element name="author" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="genre" type="string"/>
<xs:element name="publish_date" type="xs:date"/>
<xs:element name="price" type="decimal"/>
<xs:element name="description" type="xs:string"/>
</sequence>
</xs:complexType>
答案 0 :(得分:0)
是的,有一些错误。例如,<sequence>
元素应为<xs:sequence>
,类型属性也应使用xs
前缀:<xs:string>
和<xs:decimal>
。
我想你也想声明id
属性,可能它应该是一个必需的唯一身份。所以,这个架构是你想要的:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="author" type="xs:string" />
<xs:element name="title" type="xs:string" />
<xs:element name="genre" type="xs:string" />
<xs:element name="publish_date" type="xs:date" />
<xs:element name="price" type="xs:decimal" />
<xs:element name="description" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
需要<xs:attribute>
声明以允许id
元素上的<book>
属性。
答案 1 :(得分:0)
@四十二已经提供了你的XSD的good review。
要专门回答属性的使用,通常可以遵循适用于建模数据结构的相同原则。 属性适合考虑(通过子元素):
当然,阅读整个primer是一个好主意,可以看到内容模型的具体例子。