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>

In reply to Re: parsing xml file using LIBXML by mirod
in thread parsing xml file using LIBXML by Madam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.