1

Below is the sample xml which has multiple <rulex> that starts with sequence 1 and it can end up to many rule like <rule1> , <rule2>, <rule3> etc....

<?xml version="1.0" encoding="UTF-8"?>
<AddressChange_181>
    <rules>
        <rule1>
            <conditions>xya</conditions>
            <response_path>abc</response_path>
        </rule1>
        <rule2>
            <conditions>xxxx</conditions>
            <response_path>aaaa</response_path>
        </rule2>
        <rule3>
            <conditions>yyyyy</conditions>
            <response_path>ffff</response_path>
        </rule3>
        <rule4>
            <conditions>zzzz</conditions>
            <response_path>yyyy</response_path>
        </rule4>
        <default>
            <response_path>uuuuu</response_path>
        </default>
    </rules>
</AddressChange_181>

Below is the schema where i tried creating dynamic <rulex> element name for the above xml. when i generate xml from this schema i do not get the same xml format as above xml. can you please let me know how to create schema with multiple element name that starts with a sequence number. My requirement is to add more than one rule (<rule1>,<rule2>,<rule3> etc...) in xml file and this xml file should be validated against schema.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="mock_rule_list"> 
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="rules">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>        
        <xs:element ref="default" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>   
  </xs:element>

  <xs:element name="rule">    
    <xs:complexType>  
    <xs:simpleContent>
      <xs:restriction base="xs:anyType">
        <xs:pattern value="rule/d{1,3}"></xs:pattern>
      </xs:restriction>
    </xs:simpleContent>      
      <xs:sequence>
        <xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
        <xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>   

  </xs:element>

  <xs:element name="default">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="response_path"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="conditions" type="xs:string">   
  </xs:element>
  <xs:element name="response_path" type="xs:string"/>
</xs:schema>

Thanks, Madhu

1 Answer 1

1

There is no way to define such a structure using XSD if the number of ruleX tags is arbitrarily defined. If you can constrain the upper bound to a maximum, and you really have to stick with ruleX naming convention, than you can define a complex type such as ruleType, then a bunch of rule1, rule2,... , ruleN elements of that type - I would call this messy... I wouldn't recommend this.

XSD (with a maximum of 6):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="mock_rule_list">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="rules">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rule1" minOccurs="0"/>
                <xs:element ref="rule2" minOccurs="0"/>
                <xs:element ref="rule3" minOccurs="0"/>
                <xs:element ref="rule4" minOccurs="0"/>
                <xs:element ref="rule5" minOccurs="0"/>
                <xs:element ref="rule6" minOccurs="0"/>
                <xs:element ref="default"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>


    <xs:complexType name="ruleType">
        <xs:sequence>
            <xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="default">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="response_path"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="conditions" type="xs:string">
    </xs:element>
    <xs:element name="response_path" type="xs:string"/>

    <xs:element name="rule1" type="ruleType"/>
    <xs:element name="rule2" type="ruleType"/>
    <xs:element name="rule3" type="ruleType"/>
    <xs:element name="rule4" type="ruleType"/>
    <xs:element name="rule5" type="ruleType"/>
    <xs:element name="rule6" type="ruleType"/>
</xs:schema>

Alternatively, you could have a tag named "rule" with a @sequence attribute.

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="mock_rule_list">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="rules">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="default"/>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="SequenceKey">
            <xs:selector xpath="rule"/>
            <xs:field xpath="@sequence"/>
        </xs:unique>
    </xs:element>

    <xs:complexType name="ruleType">
        <xs:sequence>
            <xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
        <xs:attribute name="sequence" type="xs:int" use="required"/>
    </xs:complexType>

    <xs:element name="default">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="response_path"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="conditions" type="xs:string">
    </xs:element>
    <xs:element name="response_path" type="xs:string"/>

    <xs:element name="rule" type="ruleType"/>
</xs:schema>

Sample XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<rules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <rule sequence="1">
        <conditions>conditions1</conditions>
        <response_path>response_path1</response_path>
    </rule>
    <rule sequence="2">
        <conditions>conditions2</conditions>
        <response_path>response_path2</response_path>
    </rule>
    <default>
        <response_path>response_path1</response_path>
    </default>
</rules>

Or it could also be that one could simply use the index of the <rule/> within the parent collection .

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

1 Comment

@Gardea , Thank you ... i will change my xml template in that case.

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.