-1

recently i encountered a situation in a configuration file where i need to have an attribute with a value from a list of values specified in my document.

e.g

<SeverityList>
    <Severity name="SAFETY" />
    <Severity name="ERROR" />
    <Severity name="WARNING" />
    <Severity name="INFO" />
</SeverityList>

this list of severities comes as a part of the system configurations but is subject to change from user to user, and during the lifetime of the hospital.

later on in the same XML document, i have tests, that each has a Severity, this severity MUST BE one of the options above.

e.g.

<Test name="patient_dosage_test" severity="SAFETY" /> <!-- O.K -->
<Test name="room_temperature" severity="WTF_SEV" /> <!-- FAIL Validation -->

I would like to emphasize:
1. The Severity list is subject to change
2. The Severity list is used in XSLT to reformat something later on.

Thanks

Tomer W
  • 3,395
  • 2
  • 29
  • 44

1 Answers1

1

Hello you could define an XSD schema for this and use it to validate your model.

A sample of how to declare a key and a key reference in the schema is shown below.

    <xsd:key name="severity">
            <xsd:selector xpath="..."/>
            <xsd:field xpath="@name"/>
    </xsd:key>

    <xsd:keyref name="severity_constraint" refer="...:severity">
        <xsd:selector xpath="..."/>
        <xsd:field xpath="@severity"/>
    </xsd:keyref>

Note that for the representation of the list to that schema you might consider using a

<xs:complexType ...

EDIT: Note that your root element of your XML should have the following attributes so as to apply the validation.

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation='pathToYourFile.xsd'
Community
  • 1
  • 1
nmargaritis
  • 859
  • 7
  • 21
  • thanks, working perfectly. can this be combined with a list datatype attribute? – Tomer W Feb 18 '15 at 19:11
  • @TomerW I noticed that your severityList seems more like an enumeration. You could use complexTypes to declare this. Or check this http://stackoverflow.com/questions/8925706/xml-schema-how-to-restrict-attribute-by-enumeration – nmargaritis Feb 18 '15 at 22:25
  • i did, but then there was the requirement to have it altered by the customer in the XML. – Tomer W Feb 19 '15 at 04:43
  • Then working with the key/keyref should do the job. As this is the purpose of using them. To declare references between entities. If the Test holds a severity attribute then it should reference an existing severity. What you are asking here is if you apply the reference to the severityList instead of the severityElement ? – nmargaritis Feb 19 '15 at 08:04
  • you answered my question perfectly, i didnt know about key/ref... and had trouble searching for it. Thanks. – Tomer W Feb 19 '15 at 08:17
  • can this be tweaked to validate a values in xsd:list? – Tomer W Feb 19 '15 at 08:22
  • I am not sure about it. I believe that the representation of the list plays the key role on that. Since the severity elements will be included all in another element and be separated with space. (xsd:list) So will the validation work like this? Probably needs to be answered in practice. – nmargaritis Feb 19 '15 at 12:34