in reply to parsing xml file using LIBXML

First your file is not XML. Attributes should be quoted, so <step name= Preheat oven to 350 degrees/> will cause the parser to die.

Even if you fix this, I think your schema (or DTD or whatever) is not adapted. You use attributes where you should be using elements: name should be an element, possibly with an id attribute that would allow you to refer to it. Then you pack things in attributes that you will have to parse further: <dependency use="vanilla,nuts,!mango"/> should probably be written along the lines of: <dependency><use>vanilla</use><use>nuts</use><use type="optional">mango</use></dependency>.

Once you do this, it will be much easier for you to query the document using XPath (read on XPath, it should allow you to get directly the information you want, instead of using the 3 nested loops you have in your code). For multiple use options, I would use something like Getopt::Long and allow multiple use options, from which you can then build the proper XPath query.

It might even make sense to separate further and have the use element be an empty element with an idref to an element that will have the name of the ingredient:

<dependency><use idref="vanilla"/> <use idref="nut"/><use type="optional" idref="mango"/> </dependency>
further in the file (or in an other)
<ingredients> <ingredient id="vanilla"><name>vanilla</name><description>...</descr +iption></ingredient> <ingredient id="nut"><name>nuts</name><description>...</description> +</ingredient> <ingredient id="mango"><name>mango</name><description>...</descripti +on></ingredient> </ingredients>

Replies are listed 'Best First'.
Re^2: parsing xml file using LIBXML
by Tanktalus (Canon) on May 12, 2005 at 21:12 UTC

    Disclaimer: I like XML::Twig. A lot. But I really don't know anything about proper XML design. I just use XML that is defined by other programs and, thus, by other people.

    "Adapted." That's a new term to me. Do you have any URLs or anything (even if it's a link on xmltwig.org) on proper XML document design? I've never seen anything that describes when something should be an attribute vs a new subelement. I'd love to see something like that to learn from - so far, I just go by gut feel, and am probably wrong 50-70% of the time compared to "standard usage" :-)

      Sorry for the "adapted", it's from the French. Replace by "designed" and I hope it will make more sense.

      As for the old "attribute vs element" question. It has been the subject of countless threads, discussions, arguments... Instead of adding mine, I'll refer you to SGML/XML: Using Elements and Attributes, the commentaries are very interesting.