Adding to a given XML the XSLT stylesheet declaration with perl + LibXML

rlcsoldier

New Member
Premise: I come from C++ and I'm pretty noob at Perl.I'm trying to add a stylesheet declaration to a given \[code\].xml\[/code\] file. The \[code\].xml\[/code\] file is created by a third part and downloaded aside; we are not questioning correctness nor well-formedness of the XML. We also can't know if the XML in the file is indented or in one-row.Not being able to manipulate the file neatly only with Perl, I took XML::LibXML but I'm still stuck. This is so far what I've done.\[code\]#!/usr/bin/perluse strict;use warnings;use XML::LibXML;my $path = './file.xml';my $fxml = XML::LibXML::Document->new('1.0','utf-8');my $pi = $fxml->createPI("xml-stylesheet");$pi->setData(type=>'text/xsl', href=http://stackoverflow.com/questions/12745373/>'trasf.xsl');$fxml->appendChild($pi);$XML::LibXML::skipXMLDeclaration = 1;my $docwodecl = XML::LibXML::Document->new;$docwodecl = $doc->toString;open my $out_fh, '>', $path;print {$out_fh} $final_xml.$docwodecl;close $out_fh;\[/code\]With this I only get the XML without the initial declaration \[code\]<?xml version="1.0" encoding="ISO-8859-1"?>\[/code\] and the utf-8 characters are all messed up.I've tried to use something like this\[code\]$fxml->setDocumentElement($doc);$fxml->toFile($path);\[/code\]but it doesn't work.There's some method i could use to accomplish my (after all pretty simple) goal? I've looked in the docs but I can't find anything useful.
 
Back
Top