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 ?
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).
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]
[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.
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
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 !
Congratulations on your successful perversion of the mechanism...
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
Who's upset? Just calling a spade a spade!
I'm upset You called me a pervert ...
Rather the creator of a perversion ... unless, of course, there is something else you would like us to know about?
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.
Ah, but then we get to the hard part ... "go, and sin no more!"