A (compact-syntax) RelaxNG grammar to express what the question describes could be written as:
start = element body { h1?, h2?, p.paragraph1?, p.paragraph2?, p.paragraph3? }
h1 = element h1 { text & attribute class { string } }
h2 = element h2 { text & attribute class { string } }
p.paragraph1 = element p { text & attribute class { string "paragraph1" } }
p.paragraph2 = element p { text & attribute class { string "paragraph2" } }
p.paragraph3 = element p { text & attribute class { string "paragraph3" } }
Expressed in the RelaxNG XML syntax:
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="">
<start>
<element name="body">
<optional>
<ref name="h1"/>
</optional>
<optional>
<ref name="h2"/>
</optional>
<optional>
<ref name="p.paragraph1"/>
</optional>
<optional>
<ref name="p.paragraph2"/>
</optional>
<optional>
<ref name="p.paragraph3"/>
</optional>
</element>
</start>
<define name="h1">
<element name="h1">
<interleave>
<text/>
<attribute name="class">
<data type="string"/>
</attribute>
</interleave>
</element>
</define>
<define name="h2">
<element name="h2">
<interleave>
<text/>
<attribute name="class">
<data type="string"/>
</attribute>
</interleave>
</element>
</define>
<define name="p.paragraph1">
<element name="p">
<interleave>
<text/>
<attribute name="class">
<value type="string">paragraph1</value>
</attribute>
</interleave>
</element>
</define>
<define name="p.paragraph2">
<element name="p">
<interleave>
<text/>
<attribute name="class">
<value type="string">paragraph2</value>
</attribute>
</interleave>
</element>
</define>
<define name="p.paragraph3">
<element name="p">
<interleave>
<text/>
<attribute name="class">
<value type="string">paragraph3</value>
</attribute>
</interleave>
</element>
</define>
</grammar>
`` to be the first ``p`` element in ``body`` and thus not be preceded by an element marked with the class ``paragraph1``. Take the OP's example XML and delete the first ``p`` element, and it will validate. Seems to me it is not just the order that the OP is trying to get right but also to have a consecutive sequence which starts from 1. @Senthil please chime in.
– Louis May 17 '17 at 12:50even paragraph1 not present, this is because all the p are declared as optional. If we take the before each p, then it will ask for paragraph1 first.
– VSe May 18 '17 at 03:37title
subtitle
Para text 2
para text 1
Para text 2
` – VSe May 22 '17 at 10:46