Subject:Nesting of records from fixed length file conversion Author:Fred Foo Date:15 Jul 2012 07:46 PM Originally Posted: 12 Jul 2012 10:25 PM
I'm using a custom converter to transform a fixed field layout file to xml. The sample input file has different record types, A and B. I've seen from some of the other posts how to use pattern matching to differentiate the record types, and that works well. What I can't seem to do is nest the B records within the A records (B recs should be a child of A). This is the input:
A1234567Diane
B100.00Shoes
B123.00Suit
A1234567Chris
B100.00Saucepan
B123.00Frying Pan
This is the output I'm getting from the conversion:
<ROOT>
<A>
<ACCTNO>1234567</ACCTNO>
<NAME>Diane</NAME>
</A>
<B>
<AMOUNT>100.00</AMOUNT>
<DESCRIPTION>Shoes</DESCRIPTION>
</B>
<B>
<AMOUNT>123.00</AMOUNT>
<DESCRIPTION>Suit</DESCRIPTION>
</B>
<A>
<ACCTNO>1234567</ACCTNO>
<NAME>Chris</NAME>
</A>
<B>
<AMOUNT>100.00</AMOUNT>
<DESCRIPTION>Saucepan</DESCRIPTION>
</B>
<B>
<AMOUNT>123.00</AMOUNT>
<DESCRIPTION>Frying Pan</DESCRIPTION>
</B>
</ROOT>
Is there a way of specifying the hierarchy of the output nodes in a custom conversion to xml? Here's what it should look like:
<ROOT>
<A>
<ACCTNO>1234567</ACCTNO>
<NAME>Diane</NAME>
<B>
<AMOUNT>100.00</AMOUNT>
<DESCRIPTION>Shoes</DESCRIPTION>
</B>
<B>
<AMOUNT>123.00</AMOUNT>
<DESCRIPTION>Suit</DESCRIPTION>
</B>
</A>
declare function local:tx($tx as element(*, xs:untyped)? ) as element(*, xs:untyped)*
{
let $next := $tx/following-sibling::*[1]
return
if (local-name($next) = 'TXN' ) then
(
$next,
local:tx($next)
)
else ()
};
<ROOT>
{
for $cust in $doc/ROOT/CUST
return
<CUST>
{
$cust/*
}
<Transactions>
{
local:tx($cust)
}
</Transactions>
</CUST>
}
</ROOT>