简体   繁体   中英

XML Schema: Sequence of elements with attribute values?

Really struggling with xsd schema. I have an xml snippet as shown below

<?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. The ID column should be mandatory and I don't want to use 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

<?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

<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. So you can't have different rules for <column name="DISPLAY"> and <column name="VALUE"> . To do that, you need the feature of "conditional type assignment" in XSD 1.1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM