Newbie XML Question

Bill Marriott wjm at wjm.org
Mon Dec 13 15:29:51 EST 2004


Hi Everyone!

I'm just warming up on the Revolution XML library, and I would love the 
groups's input on how to handle a particular problem I'm trying to figure 
out.

Hypothetical Scenario:

Bob runs a grocery store with two cash registers. Only one is running at any 
given time. But sometimes he uses register A and sometimes he uses register 
B. The registers happen to spit out receipts in XML. The recipts look like 
this:

<Register Station="A">
    <Purchase Date="12/13/2004" Time="14:26:03" DateTime="20041213142603" 
Order="1">
        <Buyer CustomerID="1234" />
        <Item UPC="04905004" Price="0.40"/>
        <Description>Cherry Coca-Cola</Description>
    </Purchase>
    <Purchase Date="12/13/2004" Time="14:26:14" DateTime="20041213142614" 
Order="1">
        <Buyer CustomerID="1234" />
        <Item UPC="03424005" Price="0.65"/>
        <Description>Hershey's Chocolate Bar</Description>
    </Purchase>
    <Purchase Date="12/13/2004" Time="15:09:25" DateTime="20041213150925" 
Order="2">
        <Buyer CustomerID="4567" />
        <Item UPC="02880125" Price="6.95"/>
        <Description>Marlboro Cigarettes</Description>
    </Purchase>
</Register>

In other words, Bob had two customers. The first customer bought two items 
(Order 1), and the second customer bought one item (Order 2).

Each register spits out a tape like this, starting with Order=1 and 
continuing until the store closes. At the end of the day, Bob would like to 
feed these tapes into his computer running Revolution and get one "tape" of 
all his items, re-numbered sequentially.

* Bob can't just tape them together (append them) because the Order numbers 
would conflict. Each register starts numbering at "1" when the store opens.
* There will never be overlapping orders; only one cash register is active 
at any given time. (Bob is a sole proprietor who runs from one to the 
other.)

My approach to this would be something like this:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"on mouseup"
put tape A into XMLTree called aTree
put tape B into XMLTree called bTree
make a new, empty XML tree called combinedTree

put the total number of orders in aTree into aOrders
put the total number of orders in bTree into bOrders

put 1 into newOrderNumber
put 1 into aCompare
put 1 into bCompare

repeat while (aCompare <= aOrders) and (bCompare <= bOrders)

    put the block of purchases related to order aCompare in aTree into 
aBlock
    put the block of purchases related to order bCompare in aTree into 
bBlock

    if the earliest DateTime in aBlock < the earliest DateTime in bBlock, 
then
        put aBlock after combinedTree
        put newOrderNumber into the Order attribute for the Purchase nodes 
of that block
        increment newOrderNumber by 1
        increment aCompare by 1
    else
        put bBlock after combinedTree
        put newOrderNumber into the Order attribute for the Purchase nodes 
of that block
        increment newOrderNumber by 1
        increment bCompare by 1
    end if

    if aCompare > aOrders then
        put the rest of bTree after combinedTree
        exit repeat
    end if

    if bCompare > bOrders then
        put the rest of aTree after combinedTree
        exit repeat
    end if

end repeat
write out combinedTree
"end mouseup"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ok, so I hope my "FunkyTalk" hyperscripting isn't too obscure.

The problem is, I don't know how to manipulate blocks of XML like this. Do I 
have to convert the whole XML file into some kind of table first? Do I have 
to walk through each and every node and every attribute? Is there nothing 
like "sort lines of foobar ascending by item 5 of each?" Actually, I don't 
even know how to walk through the nodes since the functions only seem to 
return the first instance...

I really would like to do something like select/extract all the Purchases 
where the Order ID = n and bring along all the related attributed and child 
elements. But I haven't a clue how to do that.

I've gotten as far as I have through reading the "XML Demo 1" stack and "XML 
construction kit" but I've hit a wall now. Some helpful advice would be very 
welcome... and posisbly helpful for other XML newbies on the list.

Bill 



More information about the use-livecode mailing list