[英]XML Schema: Sequence of elements with attribute values?
Really struggling with xsd schema. 真正在xsd模式中挣扎。 I have an xml snippet as shown below 我有一个xml片段,如下所示
<?xml version='1.0' encoding='UTF-8'?>
<testing version="0.1">
<alarms>
<alarm ID="1">
<column name="TYPE">HIGH TEMP</column>
<column name="DISPLAY">High temperature alarm</column>
<column name="VALUE">245.66</column>
</alarm>
<alarm ID="2">
<column name="TYPE">HUMIDITY</column>
<column name="DISPLAY">Humidity alarm</column>
<column name="VALUE">56.44</column>
</alarm>
</alarms>
I have used a few online tools to generate xsd schema for this, however they are not what I was hoping for. 我已经使用了一些在线工具来为此生成xsd模式,但是它们不是我想要的。 The ID column should be mandatory and I don't want to use globals ?. ID列应该是强制性的,我不想使用globals?。
I also want to place restrictions on the values (ie validate as decimal) how do I do that 我也想对值进行限制(即验证为十进制)我该怎么做
This is what I have so far, I dont know how to validate the columns elements 这是到目前为止,我不知道如何验证column元素
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="alarms">
<xs:complexType>
<xs:sequence>
<xs:element name="alarm" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="column">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Regards 问候
First, you want a "complex type with simple content". 首先,您需要一个“具有简单内容的复杂类型”。 Searching for that phrase should be enough to find examples. 搜索该短语应该足以找到示例。 Here's an example I found here: 这是我在这里找到的示例:
c# serialization - complex type containing simple content with attributes C#序列化-包含具有属性的简单内容的复杂类型
<xsd:complexType name="AwkwardChild">
<xsd:simpleContent>
<xsd:extension base="tt:AwkwardChildType">
<xsd:attribute name="id" type="xsd:ID"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
Secondly, with XSD 1.0, if two sibling elements have the same name then they must have the same type: validation is driven entirely by element names. 其次,在XSD 1.0中,如果两个兄弟元素具有相同的名称,则它们必须具有相同的类型:验证完全由元素名称驱动。 So you can't have different rules for <column name="DISPLAY">
and <column name="VALUE">
. 因此,您不能对<column name="DISPLAY">
和<column name="VALUE">
使用不同的规则。 To do that, you need the feature of "conditional type assignment" in XSD 1.1. 为此,您需要XSD 1.1中的“条件类型分配”功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.