"I like to find solution for related tables."

That's not a Perl request. That is a database design request. And with your current design, i don't see how you can get what you want, but given the data from your second table (which i will call bb) and this data for the first table (which i will call aa):
mysql> select * from aa;
+------+--------+--------+--------+
| id   | field1 | field2 | field3 |
+------+--------+--------+--------+
|    1 | f      | b      | b      |
|    2 | q      | d      | b      |
+------+--------+--------+--------+
2 rows in set (0.00 sec)
You can use the following SQL statement to join the two tables:

SELECT aa.id, aa.field1, aa.field2, aa.field3,
       bb.type, bb.value
FROM aa
INNER JOIN bb ON aa.id = bb.id;
This yields:
+------+--------+--------+--------+------+-------+
| id   | field1 | field2 | field3 | type | value |
+------+--------+--------+--------+------+-------+
|    1 | f      | b      | b      | X    | a     |
|    1 | f      | b      | b      | X    | p     |
|    1 | f      | b      | b      | Y    | m     |
|    2 | q      | d      | b      | Z    | a     |
|    2 | q      | d      | b      | X    | a     |
+------+--------+--------+--------+------+-------+
5 rows in set (0.00 sec)
(looks like i got some of your data wrong :P ... anyhoo)

Why the dupes? Because of the design. If you really want that ID only once, you will have to either fix your design or use a module that forces allows you to intervene with the XML creation process. Yuck. I'd rather fix the design then code something like:

use strict; use warnings; use DBI; use XML::Writer; use Data::Dumper; my $dbh = DBI->connect( ... ); my $writer = XML::Writer->new(); $writer->xmlDecl('UTF-8'); $writer->doctype('xml'); $writer->startTag('xml'); my $aa_sql = 'select id,field1,field2,field3 from AA'; my $bb_sql = 'select type, value from BB where id = ?'; for my $row (@{$dbh->selectall_arrayref($aa_sql)}) { $writer->startTag('foo',id => $row->[0]); $writer->dataElement("field$_",$row->[$_]) for 1..3; # warning: i should check for 'bars' before i start a tag $writer->startTag('bars'); for $_ (@{$dbh->selectall_arrayref($bb_sql,undef,$row->[0])}) { $writer->dataElement('bar',$_->[1],type => $_->[0]); } $writer->endTag('bars'); $writer->endTag(); } $writer->endTag('xml'); $dbh->disconnect;
But again, since i don't know what you are trying to do (man you sure are cryptic in your questions!) ... i coded something silly (again).

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: XML Conversion of Tables by jeffa
in thread XML Conversion of Tables by artist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.