【发布时间】:2014-11-19 22:16:23
【问题描述】:
大家好,
情况是这样的:
我有一个 XML 文件,其中包含一个程序的许多 (>50) 配置属性。
这个 XML 内容看起来像这样:
<GlobalConfig>
<ConfigurationSettings>
<Property Name="UseColors" Value="True" Visible="False"/>
<Property Name="TitleMenu" Value="Configurator" Visible="True"/>
<Property Name="InformationText" Value="For information please read readme.txt" Visible="True"/>
[many more...]
</ConfigurationSettings>
</GlobalConfig>
我现在想做的是创建一个 xsd 文件来验证这些东西。
对于每个属性,相应的 value-attribute 具有不同的 contenttype(具有某些限制,例如 enums 或 regEx),因此 value 内容的验证规则需要适应每种情况。大小写由“名称”-属性决定
我是 xsd 主题的新手,所以我尝试了一些开始:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GlobalConfig">
<xs:complexType>
<xs:all>
<xs:element name="ConfigurationSettings">
<xs:complexType>
<xs:all>
<xs:element name="Property" minOccurs="0" maxOccurs="unbound">
<xs:complexType>
<xs:attribute name="Name" fixed="UseColors" type="xs:string"/>
<xs:attribute name="Value" default="True" type="xs:bool"/>
<xs:attribute name="Visible" default="False" type="xs:bool"/>
</xs:complexType>
</xs:element>
<xs:element name="Property" minOccurs="0" maxOccurs="unbound">
<xs:complexType>
<xs:attribute name="Name" fixed="TitleMenu" type="xs:string"/>
<xs:attribute name="Value" default="Title" type="xs:string"/>
<xs:attribute name="Visible" default="True" type="xs:bool"/>
</xs:complexType>
</xs:element>
<xs:element name="Property" minOccurs="0" maxOccurs="unbound">
<xs:complexType>
<xs:attribute name="Name" fixed="InformationText" type="xs:string"/>
<xs:attribute name="Value" default="See reedme.txt" type="xs:string"/>
<xs:attribute name="Visible" default="True" type="xs:bool"/>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute type="xs:string" name="Force"/>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
这不起作用,我想我明白为什么它不起作用: 问题是,我有许多同名的元素(“属性”)。
我有一种朦胧的感觉,这种方式实际上根本行不通,需要更改 configuration-xml 的结构,以便每个属性都有不同名称的元素。
我认为的原因是以下帖子中的第二个答案。这似乎满足了提问者的需求,但他只有两个同名的元素,他也不想检查它们的属性: XML Schema for sequence of elements with same name but different attribute values?
您同意它不适用于给定的 configuration-xml 结构吗?还是还有可能?
非常感谢!
【问题讨论】:
-
您可以在 XSD 1.1 中使用
xs:alternativeon@name解决它。 XSD 1.0 中没有标准解决方案。请参阅我发布的重复链接。
标签: xml xsd xml-validation