Empty tables in XML

Posted by jmls on 28-Aug-2009 05:34

We have an xml file produced by a prodaset using WRITE-XML

<customer>
details
<orders>
   some orders
</orders>
</customer>
<customer>
details
</customer>

that may or may not have some orders for a customer. A supplier has requested that if there are no orders, that we still add an empty section

<customer>
details
<orders>
   some orders
</orders>
</customer>
<customer>
details
<orders>
</orders>
</customer>

Is this possible using WRITE-XML on a prodataset ?

All Replies

Posted by Robin Brown on 28-Aug-2009 12:21

Hi Julian,

Per the XML layout you've provided, you have a ProDataSet with three tables: Customer, orders, and "some orders".

You could achieve the XML you want by always creating an orders record for each customer, but not any "some orders" records when there are no orders.

Below is some sample ABL which I believe gets you what you're looking for.  You need 10.2A to make use of FOREIGN-KEY-HIDDEN on the DATA-RELATION, but you can also use XML-NODE-TYPE "HIDDEN" on the fields you do not want to show up in the XML output.

Hope this helps,

Robin

DEFINE TEMP-TABLE Customer NO-UNDO
    FIELD CustNum AS INTEGER
    FIELD NAME AS CHARACTER
    INDEX iCustNum IS PRIMARY UNIQUE CustNum.

DEFINE TEMP-TABLE Orders NO-UNDO
    FIELD CustNum AS INTEGER.

DEFINE TEMP-TABLE SomeOrders NO-UNDO
    FIELD OrderNum AS INTEGER
    FIELD CustNum AS INTEGER
    FIELD OrderDate AS DATE.

DEFINE DATASET CustOrders FOR Customer, Orders, SomeOrders
         DATA-RELATION CustOrders FOR Customer, Orders
             RELATION-FIELDS (CustNum, CustNum) NESTED FOREIGN-KEY-HIDDEN
         DATA-RELATION Orders FOR Orders, SomeOrders
              RELATION-FIELDS (CustNum, CustNum) NESTED FOREIGN-KEY-HIDDEN.

CREATE Customer.
ASSIGN Customer.CustNum = 1
       NAME = "first customer".

CREATE Orders.
ASSIGN Orders.CustNum = 1.

CREATE SomeOrders.
ASSIGN SomeOrders.CustNum = 1
       SomeOrders.OrderNum = 1
       SomeOrders.OrderDate = TODAY.

CREATE SomeOrders.
ASSIGN SomeOrders.CustNum = 1
       SomeOrders.OrderNum = 2
       SomeOrders.OrderDate = TODAY.


CREATE Customer.
ASSIGN Customer.CustNum = 2
       NAME = "second customer".

CREATE Orders.
ASSIGN Orders.CustNum = 2.

DATASET CustOrders:WRITE-XML("file", "CustOrders.xml", TRUE).

Posted by jmls on 28-Aug-2009 12:31

Sorry, when I said "some orders" I meant data of the table. So I only in fact have two tables - Customer and Order. If you exclude the fields of data (for brevity) I currently have

     [Customer Data]

 

     [order data]

 

 

     [order data]

 

However, there are some times where there is no orders for the customer. This file currently looks like this:

     [Customer Data]

What my supplier is requesting is that I send the file like this:

     [Customer Data]

 

 

Why ? I don't know I have a meeting set up next week, and want to be able to tell them what I can and can't do.

Posted by Robin Brown on 28-Aug-2009 12:51

Sorry for my mis-interpretation.

For WRITE-XML to write out the tags, there has to be an order record for the customer.  You might be able to use omit-initial-values in 10.2A on WRITE-XML method, and create a 'dummy' record containing all initial values (except for the foreign key of the data-relation), but then any consumer of that XML would have to know how to interpret this.  The only options in ABL that I see would be to use DOM or SAX and write your own.

Regards,

Robin

Posted by jmls on 28-Aug-2009 14:05

rbrown wrote:

Sorry for my mis-interpretation.

For WRITE-XML to write out the tags, there has to be an order record for the customer.  You might be able to use omit-initial-values in 10.2A on WRITE-XML method, and create a 'dummy' record containing all initial values (except for the foreign key of the data-relation), but then any consumer of that XML would have to know how to interpret this.  The only options in ABL that I see would be to use DOM or SAX and write your own.

Regards,

Robin

A little lateral thinking has solved this for me

Create a third table called NoOrders, as follows:

    DEF TEMP-TABLE NoOrders NO-UNDO XML-NODE-NAME "Orders"
      FIELD CustomerID AS INT XML-NODE-TYPE "HIDDEN"
     
      INDEX CustomerId IS PRIMARY
          CustomerID.

Add this table to the dataset. If there are no orders for the customer, create a NoOrders record instead. When the XML is produced, the node-name is "Orders" (not NoOrders) but because the CustomerID in NoOrders is hidden, an empty pair is produced ! And, because there is no NoOrders record for the customers that have an order, the "proper" Orders tags are produced with data.

Woohoo. Thanks for the pointers !

Posted by Thomas Mercer-Hursh on 28-Aug-2009 14:28

Congratulations on your successful perversion of the mechanism...

Posted by jmls on 28-Aug-2009 14:36

tamhas wrote:

Congratulations on your successful perversion of the mechanism...

Hey, it wouldn't be fun if I didn't upset you once in a while

Posted by Thomas Mercer-Hursh on 28-Aug-2009 14:44

Who's upset?  Just calling a spade a spade!

Posted by jmls on 28-Aug-2009 14:48

I'm upset You called me a pervert ...

Posted by Thomas Mercer-Hursh on 28-Aug-2009 15:08

Rather the creator of a perversion ... unless, of course, there is something else you would like us to know about?

Posted by jmls on 28-Aug-2009 15:14

Oh. Ok. Ummmm. Ah....  Well....... Maybe the time has come to confess ... I ... did ... (they made me do it) .... once  .... use

(oh! the shame!)

SHARED VARIABLES

There! I've done it ! I'm freeeeee ! Thanks to the Doctor! my burden is lifted.

Posted by Thomas Mercer-Hursh on 28-Aug-2009 15:47

Ah, but then we get to the hard part ... "go, and sin no more!"

This thread is closed