Home > Net >  Flexible recursive XML schema
Flexible recursive XML schema

Time:12-22

I'm trying to create a validation schema for this sample XML. I already tried using recursive definitions, but cannot find a proper way. Please note that IF and ELSE elements can be nested any number of times, and ACTIONx elements can appear in any order. Also note that every element has its own possibly different attributes.

<?xml version="1.0" encoding="utf-8"?>
<KEYPRESS Buffer="1">
    <ACTION1 Index="15" />
    <IF Condition="0">
        <IF Condition="1">
            <ACTION1 Index="14" />
            <ELSE>
                <ACTION2 Measure="whatever" />
            </ELSE>
        </IF>
    
        <IF Condition="2">
            <IF Condition="5">
                <ACTION2 Measure="whatelse" />
                <ACTION3 Type="Flag"  />
            </IF>
            <ELSE>
                <ACTION1 Index="0" />
                <ACTION3 Type="Other" />
                <IF Condition="1">
                    <ACTION3 Type="Flag" />
                </IF>
            </ELSE>
        </IF>
    </IF>
</KEYPRESS> 

Any help will be appreciated.

CodePudding user response:

I do not think that you can define a schema with variable element names.

An element declaration requires a name. The value is of the type xs:NCName, which is just a string and not a pattern. In order to write a schema, you have to find out the maximum number of action elements. And you have to define a complexType for every single element. I do not think you want to do this.

You XML file has been designed by someone, who does not know much about XML. You better tell him, that the format has to be redesigned. Instead of

<ACTION1 ...>

use

<ACTION N="1" ...>

and the schema validation will be easy.

CodePudding user response:

This kind of structure is what substitution groups are for.

Define an abstract element INSTRUCTION, and then define IF, ACTION1, ACTION2, and ACTION3 as members of the substitution group of INSTRUCTION, each with its own type definition defining the permitted attributes. The content model of KEYPRESS and ELSE appears to be INSTRUCTION* (a sequence of 0 or more instructions), and the content model of IF appears to be (INSTRUCTION*, ELSE?) (a sequence of 0 or more instructions followed optionally by an ELSE).

(ELSE is not a member of the substitution group, because it can't appear anywhere an instruction appears).

  • Related