Can relaxng specify an unordered set of elements with the same name but different attributes?

Im working on automation testing an API that accepts and returns XML, so I want to translate as much as possible the documented returned API data into the schema. I chose RelaxNG for this task based on ease of use and training.

Before throwing all the information, here is the question:

Is it possible to describe an "unordered set of elements with the same name but with different attributes"?

Here is an example of an object that I am having problems describing:

<item>
    <id>d395136e-d060-4a6e-887c-c0337dd7ad09</id>
    <name>The item has a name</name>
    <link rel="self" type="type1" href="url" />
    <link rel="download" type="type2" href="url" />
    <link rel="relatedData" type="type3" href="url" />
</item>

Link objects are the bit I hung up on. Here is the problem:

  • The order of the elements inside the element is not guaranteed, so I am trying to put all the elements in a structure <interleave>.
  • <item> <link>, (.. <item> "self", "" "relatedData" )).
  • , .

:

<element name="item">
    <interleave>
        <element name="id"><text/></element>
        <element name="name"><text/></element>
        <ref name="selfLink"/>
        <ref name="launchLink"/>
        <ref name="thumbnailLink"/>
    </interleave>
</element>

'link' :

 <define name="selfLink">
 <element name="link">
     <attribute name="href"><text/></attribute>
     <attribute name="rel"><value>self</value></attribute>
     <attribute name="type"><value>type1</value></attribute>
 </element>
 </define>

- jing error: the element "link" can occur in more than one operand of "interleave". , , , " , " .

refs interleave , , , .

, ? XML, , ( XML?)

+5
2

, RELAX NG. Schematron , , RELAX NG Schematron.

, <link>, Schematron, Jing:

<schema xmlns="http://www.ascc.net/xml/schematron">
  <pattern name="link pattern">
    <rule context="item">
      <assert test='count(link) = 3'>There must be 3 link elements.</assert>
      <assert test="count(link[@rel = 'self' and @type ='type1']) = 1">There must be 1 link element wwhere @rel='self' and @type='type1'.</assert>
      <assert test="count(link[@rel = 'download' and @type ='type2']) = 1">There must be 1 link element where @rel='download' and @type='type2'.</assert>
      <assert test="count(link[@rel = 'relatedData' and @type = 'type3']) = 1">There must be 1 link element where @rel='relatedData' and @type='type3'.</assert>
    </rule>
  </pattern>
</schema>
+3

,

<grammar xmlns="http://relaxng.org/ns/structure/1.0">
<start>
    <element name="item">
        <interleave>
            <element name="id"><text/>
            </element>
            <element name="name"><text/></element>
            <oneOrMore>
                <ref name="link"/>
            </oneOrMore>
        </interleave>
    </element>
</start>

<define name="link">
    <element name="link">
        <attribute name="href"/>
        <choice>
            <group>
                <attribute name="rel"><value>self</value></attribute>
                <attribute name="type"><value>type1</value></attribute>
            </group>
            <group>
                <attribute name="rel"><value>download</value></attribute>
                <attribute name="type"><value>type2</value></attribute>
            </group>
            <group>
                <attribute name="rel"><value>relatedData</value></attribute>
                <attribute name="type"><value>type3</value></attribute>
            </group>
        </choice>
    </element>
</define>
</grammar>
+1

All Articles