我正在尝试针对我们的web.config文件创建一个xsd文件。我试图捕捉人为错误并防止标记嵌套。坏xml的例子:
<applicationSettings>
<app.MySettings>
<setting name="FirstName" serializeAs="String">
<value>John</value>
<setting name="LastName" serializeAs="String">
<value>Smith</value>
</setting>
</setting>
</app.MySettings>
</applicationSettings>
您会注意到设置&#34;姓氏&#34;嵌套在&#34; FirstName&#34;内。正确的格式是:
<applicationSettings>
<app.MySettings>
<setting name="FirstName" serializeAs="String">
<value>John</value>
</setting>
<setting name="LastName" serializeAs="String">
<value>Smith</value>
</setting>
</app.MySettings>
</applicationSettings>
所以,我试图创建一个模式来运行它。到目前为止我有这个:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="applicationSettings">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:sequence>
<xs:element name="app.mysettings">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:sequence>
<xs:element name="setting">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:sequence>
<xs:element type="xs:short" name="value"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name"/>
<xs:attribute type="xs:string" name="serializeAs"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
有些事情是错的,因为我找回了它应该不具备的有效性?如果这是错误的方法(使用架构),请告诉我一个更好的方法。
谢谢!
D H
答案 0 :(得分:1)
为上述xml实例设计架构的更好方法
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.com"
targetNamespace="http://example.com"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="applicationSettings">
<xs:complexType>
<xs:sequence>
<xs:element ref="app.MySettings"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="value" type="xs:short"/>
<xs:element name="setting">
<xs:complexType>
<xs:sequence>
<xs:element ref="value"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="serializeAs" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="name" type="xs:string"/>
<xs:element name="serializeAs" type="xs:string"/>
<xs:element name="app.MySettings">
<xs:complexType>
<xs:sequence>
<xs:element ref="setting" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答案 1 :(得分:0)
答案有点正确,完整架构是
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="applicationSettings">
<xs:complexType>
<xs:sequence>
<xs:element name="app.MySettings"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="value" type="xs:string"/>
<xs:element name="setting">
<xs:complexType>
<xs:sequence>
<xs:element ref="value"/>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="serializeAs" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="name" type="xs:string"/>
<xs:element name="serializeAs" type="xs:string"/>
<xs:element name="app.MySettings">
<xs:complexType>
<xs:sequence>
<xs:element ref="setting" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>