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!

XML - Element value becomes Element name

Status
Not open for further replies.

kamv

MIS
Jun 14, 2002
22
US
I have a simple XML schema such as:

<DDGroup>
<Data>
<Field_1>x</Field_1>
<Field_2>y</Field_2>
<Field_3>z</Field_3>
</Data>
</DDGroup>

I have a comma delimited file whose first row contains column names.(The content of the file can change per file but the first row will always contain column names.)

Ex:
Cars, Planes, Motorcycles
Ford, Boeing, Harley
Honda, Airbus, Suzuki

I would like these column names to become the Element names dynamically.Is there a way to make it so that the Schema would look like this:

<DDGroup>
<Data>
<Cars>Ford</Cars>
<Planes>Boeing</Planes>
<Boats>Harley</Boats>
</Data>
</DDGroup>
Then if I was to apply the schema to a different file that had different column names those would become the element name?

Your help Is appreciated!
 
Schema just define the rules of the XML, they don't do any processing. You have to write a script to do it from the CSV or an XSL stylesheet to do it from the XML.

Do you really want the XML to be structured like that? It's not very semantic. Surely this would be better:
Code:
<Vehicles>
  <Cars>
    <Car>Ford</Car>
  </Cars>
  <Planes>
    <Plane>Boeing</Plane>
  </Planes>
  <Motorcycles>
    <Motorcycle>Harley</Motorcycle>
  </Motorcycles>
</Vehicles>

Jon

"I don't regret this, but I both rue and lament it.
 
Hi Jon,
Thanks for the response.
You are right semantically your example is better. However I am basically trying to deal with a comma delimited file whose contents will vary quite often. The file, though, will always have the first line as the colunmn names. So I want those values to become the element names in my schema.
So in the example I gave above the file contained data abnout Vehicles, but the next file may contain data about Produce:
Ex.
Fruits, Vegies, Grain
Apple, radish, wheat

I need the XML to be able to chane with the change in the file. If I am not clear or making sense, pardon my ignorance - I am fairly new to XML :)

Thanks,
KamV
 
You could do it, but with a program written in a language such as C# that was loading the file. I don't know of any other way to dynamically write XML, but someone else here might know more than I.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top