1

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 (i.e 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

3
  • Please post the schema you already set up so far. Commented Dec 22, 2016 at 9:43
  • Thanks, just edited the post Commented Dec 22, 2016 at 10:27
  • Hi all, is it true that xml column elements must all have the same type? If so, van this xml be validated with a schema ? Commented Dec 22, 2016 at 12:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Ive edited my post, but still dont know how to validate the column text
I've added to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.