Using Perl
DB query->XML->HTML
We're building a prototype of a system to pull product
information from a rdbms, build an XML document from that
data, and finally use a series of XSLTs to transform the
XML doc into a series of web pages.
The code below ends before we get to the XSLT trick (which works).
- The problem -
Some of the data coming from the database contains some html
pieces/parts. When I use the XML::LibXML::Text->new to
build an object of the text/html, the 'Text->new' method converts all of the HTML angle brackets into
and
.
Consequently, we loose the ability to discriminate between
previously existing '<' chars and those which are the result of pulling the text/html through the 'Text->new' method.
Looking for ideas/strategies to pull the text/html and add it to the XML doc, later to be processed by the XSLTs.
I have tried pulling the data through the the XML::LibXML->parse_html_string but get a core dump when I try to appendChild to the partially built document.
#!/usr/bin/perl
use strict;
use DBI;
use XML::LibXML;
use XML::LibXSLT;
my $dbh;
&db_connect; # connect to db (MS SQL Server)
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
# start a new XML document
my $xml_doc = XML::LibXML:
ocument->new();
$xml_doc->setEncoding('UTF-8');
# main enclosing element
my $prod_report = XML::LibXML::Element->new('prod_report');
$xml_doc->setDocumentElement($prod_report);
# get data from database - an array or db rows
my @prod_data = &db_query('SELECT ProductId, Name, description, URL
FROM Products
order by ProductId');
my $products = XML::LibXML::Element->new('products');
$prod_report->appendChild($products);
foreach my $ref (@prod_data)
{
my $product = XML::LibXML::Element->new('prodid');
$product->setAttribute('id',$$ref[0]);
my $name = XML::LibXML::Element->new('name');
my $name_text = XML::LibXML::Text->new($$ref[1]);
$name->appendChild($name_text);
my $desc = XML::LibXML::Element->new('desc');
# Need to handle incoming text containing some HTML chunks.
my $desc_text = XML::LibXML::Text->new($$ref[2]);
$desc->appendChild($desc_text);
$product->appendChild($name);
$product->appendChild($desc);
$products->appendChild($product);
}
my $output_string = $xml_doc->toString;
print ($output_string);
#--------------------------------------------------------------------
# db_connect
#--------------------------------------------------------------------
sub db_connect
{
$dbh = DBI->connect("dbi:Sybase:server=servername", 'user', 'pass', {PrintError => 0});
unless ($dbh) { die "Unable for connect to server $DBI::errstr"; }
$dbh->do('use db_name');
}
#--------------------------------------------------------------------
# db_query
#--------------------------------------------------------------------
sub db_query
{
my $sql = shift;
my @data;
my $sth = $dbh->prepare("$sql"
;
if($sth->execute)
{ while(my @row = $sth->fetchrow) { push @data, [ @row ]; } }
else { print "ERROR - $DBI::errstr\n"; }
$sth->finish;
return(@data);
} 'hope this helps
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
DB query->XML->HTML
We're building a prototype of a system to pull product
information from a rdbms, build an XML document from that
data, and finally use a series of XSLTs to transform the
XML doc into a series of web pages.
The code below ends before we get to the XSLT trick (which works).
- The problem -
Some of the data coming from the database contains some html
pieces/parts. When I use the XML::LibXML::Text->new to
build an object of the text/html, the 'Text->new' method converts all of the HTML angle brackets into
Code:
'ampersand lt;'
Code:
'ampersand gt;'
Consequently, we loose the ability to discriminate between
previously existing '<' chars and those which are the result of pulling the text/html through the 'Text->new' method.
Looking for ideas/strategies to pull the text/html and add it to the XML doc, later to be processed by the XSLTs.
I have tried pulling the data through the the XML::LibXML->parse_html_string but get a core dump when I try to appendChild to the partially built document.
#!/usr/bin/perl
use strict;
use DBI;
use XML::LibXML;
use XML::LibXSLT;
my $dbh;
&db_connect; # connect to db (MS SQL Server)
my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
# start a new XML document
my $xml_doc = XML::LibXML:
$xml_doc->setEncoding('UTF-8');
# main enclosing element
my $prod_report = XML::LibXML::Element->new('prod_report');
$xml_doc->setDocumentElement($prod_report);
# get data from database - an array or db rows
my @prod_data = &db_query('SELECT ProductId, Name, description, URL
FROM Products
order by ProductId');
my $products = XML::LibXML::Element->new('products');
$prod_report->appendChild($products);
foreach my $ref (@prod_data)
{
my $product = XML::LibXML::Element->new('prodid');
$product->setAttribute('id',$$ref[0]);
my $name = XML::LibXML::Element->new('name');
my $name_text = XML::LibXML::Text->new($$ref[1]);
$name->appendChild($name_text);
my $desc = XML::LibXML::Element->new('desc');
# Need to handle incoming text containing some HTML chunks.
my $desc_text = XML::LibXML::Text->new($$ref[2]);
$desc->appendChild($desc_text);
$product->appendChild($name);
$product->appendChild($desc);
$products->appendChild($product);
}
my $output_string = $xml_doc->toString;
print ($output_string);
#--------------------------------------------------------------------
# db_connect
#--------------------------------------------------------------------
sub db_connect
{
$dbh = DBI->connect("dbi:Sybase:server=servername", 'user', 'pass', {PrintError => 0});
unless ($dbh) { die "Unable for connect to server $DBI::errstr"; }
$dbh->do('use db_name');
}
#--------------------------------------------------------------------
# db_query
#--------------------------------------------------------------------
sub db_query
{
my $sql = shift;
my @data;
my $sth = $dbh->prepare("$sql"
if($sth->execute)
{ while(my @row = $sth->fetchrow) { push @data, [ @row ]; } }
else { print "ERROR - $DBI::errstr\n"; }
$sth->finish;
return(@data);
} 'hope this helps
If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.