Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, more of a design question, but suppose I have data like:

<inventory> <item> <name>item's name</name> <price>item price</price> </item> <item> <name>item's name</name> <price>item price</price> </item> </inventory>

I will be adding, editing, and deleting items through a web-based interface. I need a way to identify items. Using the above fields within an item (such as the name) will not work because there may be duplicates. The best I've come up with so far is adding a unique 'id' attribute to the item tags. The problems with this approach are mostly related to the generation and deletion of the item numbers. All I can think of is running a cron job to reassign item ids as other are deleted, but this seems rather sub-optimal.

Any ideas?

Replies are listed 'Best First'.
Re: Item Identification in XML Files
by pzbagel (Chaplain) on Apr 30, 2003 at 05:14 UTC

    Why are you attempting to talk yourself out of a good solution? Stores (both on-line and brick-and-mortar) use unique identifiers for their items. Things like part numbers and ISBN numbers make sure that when you order an item, the store knows exactly what you want. If you expect to have item names that are identical, how will you keep them straight, let alone your script, without a unique identifier?

    However, if you insist on not having to set per-item ids, you can simply add an extra id field to each item as you convert it to html to display on your web interface. The item's location in the file(i.e. which number it is) could be the id. When you resubmit your form, you can use this number to make sure you are modifying the correct item. This assumes that only one person changes the file at a time(since adding an item could throw the count off.)

    Later

      So you think doing something like the following would be the best solution?

      <inventory> <item id="1"> <name>item's name</name> <price>item price</price> </item> <item id="2"> <name>item's name</name> <price>item price</price> </item> </inventory>

      If so, which do you think is the best way to go about assigning the numbers. Also do you think the cron job idea is good for reassigning item ids that are currently no longer being used? Or is there a less costly method? Thanks.

        Why do you insist on re-assigning numbers? Do you have a limited supply of them ;--)

Re: Item Identification in XML Files
by grantm (Parson) on Apr 30, 2003 at 05:18 UTC

    Each item could be uniquely identified by an XPath expression eg: /inventory/item[2] would refer to the second one.

    Whether this would be a good idea or not is another question. Concurrent (or interleaved updates) could get very messy .

    Had you considered putting the data in a database? If someone really needs it in XML, you could extract it from the DB and convert it to XML at the time the need it.

      XPath wouldn't work because the items will be switching spots in the file and I need a persistant way of identifying them. I did also consider the database, but I'm using a few tools that require xml input, so the conversions to xml and back again would be too costly. Thanks for the reply though, I'm kind of just thinking out loud here, so any suggestions are appreciated :)

Re: Item Identification in XML Files
by dragonchild (Archbishop) on Apr 30, 2003 at 13:38 UTC
    Why are you trying to replicate database functionality in something that patently isn't a database? There are a million database alternatives, assuming you don't want to (or can't) use MySQL. Every single one of them will generate a unique ID for you and manage it for you. You wouldn't even have to know it's there (other than setting it up).

    Get it straight, people - XML is not a database!

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      XML is not a database!

      It will be, It will be... ;-)

Re: Item Identification in XML Files
by Aristotle (Chancellor) on Apr 30, 2003 at 14:00 UTC
    You say there "may" be duplicate names. So the name will be nearly unique. How about adding not an ID that's not globally unique, but distinguishes items with identical names from one another? You can just keep assigning larger numbers; it's unlikely you'll ever produce/stock over 4 million variants of the same product.

    Makeshifts last the longest.