Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD Ordering elements 1

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
I've been trying (rather unsuccessfully) to create an XSD that would allow for something like this:
Code:
<RuleTasks>
	<RuleTaskEnum Variable="SpecialType">
		<SubRules />
		<Value>1</Value>
		<Value>2</Value>
	</RuleTaskEnum>
	<RuleTaskEnum Variable="SpecialType1">
		<Value>1</Value>
		<Value>2</Value>
		<SubRules />
	</RuleTaskEnum>
	<RuleTaskEnum Variable="SpecialType2">
		<Value>1</Value>
		<SubRules />
		<Value>2</Value>
	</RuleTaskEnum>
</RuleTasks>
Here is something I've tried already:
Code:
<xs:complexType name="RuleTasks">
	<xs:choice minOccurs="1" maxOccurs="unbounded">
		<xs:element name="RuleTaskEnum" type="RuleTaskEnum" />
		<xs:element name="RuleTaskLength" type="RuleTaskLength" />
	</xs:choice>
</xs:complexType>

<xs:attributeGroup name="aRuleTask">
	<xs:attribute name="Variable" type="xs:string" use="required" />
	<xs:attribute name="StopProcessing" type="xs:boolean" use="optional" />
	<xs:attribute name="FailMsg" type="xs:string" use="optional" />
	<xs:attribute name="WarnNoFail" type="xs:boolean" use="optional" />
</xs:attributeGroup>
<xs:group name="gRuleTask">
	<xs:sequence>
		<xs:element name="SubRules" type="RuleContainer" minOccurs="0" maxOccurs="1" />
	</xs:sequence>
</xs:group>
<xs:complexType name="RuleTaskEnum">
	<xs:choice>
		<xs:sequence>
			<xs:group ref="gRuleTask" />
			<xs:element name="Value" type="xs:anyURI" minOccurs="1" maxOccurs="unbounded" />
		</xs:sequence>
		<xs:sequence>
			<xs:element name="Value" type="xs:anyURI" minOccurs="1" maxOccurs="unbounded" />
			<xs:group ref="gRuleTask" />
		</xs:sequence>
	</xs:choice>
	<xs:attributeGroup ref="aRuleTask" />
</xs:complexType>
As you can see I want to separate the SubRules element and the Variable attribute from RuleTaskEnum so I can use them for other task types yet to be defined (you can see one in there RuleTaskLength).

That XSD shows an error for multiple definitions of "Value".

I've also try extensions (tried this first actually):
Code:
<xs:complexType name="RuleTask">
	<xs:sequence>
		<xs:element name="SubRules" type="RuleContainer" minOccurs="0" maxOccurs="1" />
	</xs:sequence>
	<xs:attribute name="Variable" type="xs:string" use="required" />
	<xs:attribute name="StopProcessing" type="xs:boolean" use="optional" />
	<xs:attribute name="FailMsg" type="xs:string" use="optional" />
	<xs:attribute name="WarnNoFail" type="xs:boolean" use="optional" />
</xs:complexType>
<xs:complexType name="RuleTaskEnum">
	<xs:complexContent>
		<xs:extension base="RuleTask">
			<xs:sequence>
				<xs:element name="Value" type="xs:anyURI" minOccurs="1" maxOccurs="unbounded" />
			</xs:sequence>
		</xs:extension>
	</xs:complexContent>
</xs:complexType>
<xs:complexType name="RuleTaskLength">
	<xs:complexContent>
		<xs:extension base="RuleTask">
			<xs:attribute name="MinLength" type="xs:integer" use="optional" />
			<xs:attribute name="MaxLength" type="xs:integer" use="required" />
		</xs:extension>
	</xs:complexContent>
</xs:complexType>

How can I possibly get it so the SubRules element can be in any order with the other elements of the task (if there are other elements), and be decoupled from the tasks?

Please let me know if you need me to clarify anything.

Thanks.
 
You're defining Value twice, hence the error.

XML is a structured language, the subRules element needs to have some hierarchy. I'm not sure you can place it anywhere in the tree.

Cheers,
Dian
 
I realize that's why there was the error - the error said as much. As for the SubRules element needing some hierarchy - it does, I just didn't post that as it wasn't really relevant to the order of the SubRules element within it's parent (from the last code block you can see SubRules is defined as the custom type RuleContainer).

I'm trying to ascertain how to get the SubRules elements (0 or 1) and the Values elements (1 or more) to be in ANY order.

I know about the ALL definition but that only allows 0 or 1 of any element contained within it, and then I can't have multiple Value elements.
 
Is this perhaps only possible with RelaxNG?
 
If you want to put it bluntly, not what w3c schema would care to validate. You can put that part into say xs:any and have that part validate by custom routine in the application, or with Schematron in tandom, or as you said entirely using relax ng.
 
Thanks tsuji - that's what I was thinking. I've been playing around with RelaxNG and I think I got it working (I've been testing with a PHP validator).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top