denzil_cactus has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Please help me to resolve the issue here as I am trying to convert one XML file in one to another format via XML::LibXSLT module.

Input file for XSLT conversion :
<?xml version="1.0" encoding="utf-8"?> <File xmlns ="http://www.w3.org/2001/XMLSchema"> <DataDescrip> <ID>C01362023</ID> <Reference>702154</Reference> </DataDescrip> </File>
Output After XSLT Conversion:

<?xml version="1.0"?> C01362023 702154 <b>.xsl file used for Conversion</b> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" versi +on="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="File"> <Data xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsl:for-each select="DataDescrip"> <xsl:element name="Details"> <xsl:element name="ReferenceID"><xsl:value-of select="Reference"/></xs +l:element> </xsl:element> </xsl:for-each> </Data> </xsl:template> </xsl:stylesheet>

Snip of perl script for XSLT Conversion:

foreach my $file (@files) { eval('use XML::LibXML'); eval('use XML::LibXSLT'); my $oXslt = XML::LibXSLT->new(); my $oSource = XML::LibXML->load_xml(location => $file); my $oStylesheetDoc = XML::LibXML->load_xml(location => $cPath, no_cdat +a=>1); my $hXSLTDoc = $oXslt->parse_stylesheet($oStylesheetDoc); my $hOutput = $hXSLTDoc->transform($oSource); my $cFilecontent = $hXSLTDoc->output_as_bytes($hOutput); my $cConvertedFile = $file."_Converted.XML"; open(FH,">$cConvertedFile") or die "cannot open $cConvertedFile : $!\n +"; print FH $cFilecontent; close(FH); }

Replies are listed 'Best First'.
Re: Please help with the issue in XML::LibXSLT module
by haukex (Archbishop) on Apr 04, 2017 at 14:02 UTC

    In regards to the Perl code, why do you do eval('use XML::LibXML'); (without checking it for errors) when you could just do use XML::LibXML; at the top of your script?

    As for your problem, note that it is an issue with XSLT, not Perl, as you can verify by doing something like xsltproc -o output.xml transform.xsl input.xml at the command line. The problem is namespaces: note how your source document is in the namespace http://www.w3.org/2001/XMLSchema, but in your XSLT you don't take that into account. One way to do that is like this:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="xsd:File"> ...

    And using the xsd: prefix in front of the other element names too. But again, this is more of an XSLT question than a Perl question, so there are probably better suited forums elsewhere. See also the page XML Namespaces on w3schools, they also have a tutorial on XSLT.

    Update: poj already posted while I was drafting.

Re: Please help with the issue in XML::LibXSLT module
by poj (Abbot) on Apr 04, 2017 at 13:55 UTC

    Change the root and add namespace to your selects

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" versi +on="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <Data xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsl:for-each select="xsd:File/xsd:DataDescrip"> <xsl:element name="Details"> <xsl:element name="ReferenceID"> <xsl:value-of select="xsd:Reference"/> </xsl:element> </xsl:element> </xsl:for-each> </Data> </xsl:template> </xsl:stylesheet>

    gives

    <?xml version="1.0"?>
    <Data xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      
    <ReferenceID>702154</ReferenceID>
    </Data>
    poj
Re: Would like to delete the node
by 1nickt (Canon) on Apr 04, 2017 at 11:46 UTC

    Please don't. It's a historical record. If you said something dumb, oh well. We all have. Edit the post and make a correction, even use <strike></strike> tags to cross it out. But please don't delete the content.

    See How do I change/delete my post? from the PerlMonks FAQ.


    The way forward always starts with a minimal test.
Re: Would like to delete the node
by marto (Cardinal) on Apr 04, 2017 at 11:53 UTC
Re: Would like to delete the node
by haukex (Archbishop) on Apr 04, 2017 at 11:46 UTC