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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive data structure example or help

Status
Not open for further replies.

Dataman9

Programmer
Apr 27, 2002
6
US
This XML newbie is looking for some advice on how ot work with bill-of-material type data structures in XML, where a part element is a parent another another part element, etc:

<Part> (assembly)
<Part> (sub-assembly)
<Part> (component)

I'm thinking that the schema would consist of a Part element that contained a ParentPart attribute similiar to a relation DB solution:

<Parts>
<Part ParentPart=&quot;PartB&quot;>PartD</Part>
<Part ParentPart=&quot;PartB&quot;>PartC</Part>
<Part ParentPart=&quot;PartA&quot;>PartB</Part>
<Part ParentPart=&quot;Top&quot;>PartA</Part>
</Parts>

The next problem I have is how to present this data in it's hierarchically structure with a template. I'm guessing that it would need to select where the ParentPart=&quot;Top&quot; and then apply stylesheets with the Part name passed as a parameter - ParentPart = @Part.

This would need to work recursively since the number of levels in the hierarchy are unknown.

Could anyone steer me in the correct direction or post any examples of working with recursive data structures?

Thank you for your help,
Gary
 
You can also define the data to be recursive.

Example Schema:
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-15&quot;?>
<xs:schema xmlns:xs=&quot;[URL unfurl="true"]http://www.w3.org/2001/XMLSchema&quot;[/URL] elementFormDefault=&quot;qualified&quot;>
	<xs:complexType name=&quot;PartType&quot;>
		<xs:sequence>
			<xs:element name=&quot;Part&quot; type=&quot;PartType&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/>
		</xs:sequence>
		<xs:attribute name=&quot;id&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/>
	</xs:complexType>
	<xs:element name=&quot;Parts&quot;>
		<xs:complexType>
			<xs:sequence>
				<xs:element name=&quot;Part&quot; type=&quot;PartType&quot; maxOccurs=&quot;unbounded&quot;/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

This will allow easy XPath access to the structure via axis selection.

Example xml:
Code:
<Parts>
   <Part id=&quot;PartA&quot;>
      <Part id=&quot;SubPartB&quot;>
         <Part id=&quot;SubSubPartA&quot;/>
      </Part>
      <Part id=&quot;SubPartC&quot;/>
   </Part>
   <Part id=&quot;PartB&quot;>
      <Part id=&quot;SubPartD&quot;/>
   </Part>
</Parts>

Now you can e.g. select all Part children of PartA via XPath expression
Code:
//Part[@id='PartA']/descendant::Part

this will select
Code:
   <Part id=&quot;SubPartB&quot;/>
   <Part id=&quot;SubSubPartA&quot;/>
   <Part id=&quot;SubPartC&quot;/>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top