himanshu.padmanabhi has asked for the wisdom of the Perl Monks concerning the following question:

#Defining objects for XML,XSLT. my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); #Parsing both files my $source = $parser->parse_file($xmlfile); # '$style_doc' represents 'XML::LibXML::Document' object representing +an XSLT file. my $style_doc = $parser->parse_file($xslfile); my $stylesheet = $xslt->parse_stylesheet($style_doc); # 'Transform' returns a scalar that is the XSLT rendering of the 'XML: +:LibXML::Document' object using the desired output format(specified i +n the xsl:output tag in the stylesheet) my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to +_string(args1=> "$in{'args1'}",args2=> "$in{'args2'}",args3=> "$in{'a +rgs3'}",idx => "$in{'idx'}",newperiod => "$in{'newperiod'}")); # IN ABOVE CASE,I DN'T WANT TO HARDCODE 'ARGS' LIKE 3 ABOVE.I AM GETTI +NG TOTAL NUMBER OF PARAMETERS IN VARIABLE.SO IF ARGS CAN BE 5 IN SOME + CASE,ABOVE LINE SHOULD BE LIKE THIS.. # my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to +_string(args1=> "$in{'args1'}",args2=> "$in{'args2'}",args3=> "$in{'a +rgs3'}",args4=> "$in{'args4'}",args5=> "$in{'args5'}",idx => "$in{'id +x'}",newperiod => "$in{'newperiod'}")); print $stylesheet->output_string($results);
Actually I want that it should check '$in' hash.It contains '$in{args<number>}'.number is changing thing from 1 onwards.It should pass these '$in{args<number>}' variables to XSL file.

Replies are listed 'Best First'.
Re: passing variable arguments to XSL from perl
by dHarry (Abbot) on Apr 07, 2009 at 11:27 UTC

    Seems you re-posted: passing variable arguments to XSLT. What was wrong with the answer that jethro provided? I thought it solved the problem? Maybe you should post a snippet of the xml/xslt files as well.

      Ohhhh...So sorry. I thought it was not posted. Yeah..Thank you very much..It solved the problem
Re: passing variable arguments to XSL from perl
by derby (Abbot) on Apr 07, 2009 at 11:11 UTC

    Just iterate over the keys of %in to build your new arguments hash:

    ... my %in = ( arg1 => 'foo', arg2 => 'bar', arg3 => 'baz', ignore => 'this entry', idx => 'index', newperiod => "bar", ); my %xsltargs; map { $xsltargs{$_} = $in{$_} } grep /^arg|^idx$|^newperiod$/, keys %in; ... my $results = $stylesheet->transform( $source, XML::LibXSLT::xpath_to_string(%xsltargs) );

    -derby
passing variable arguments to XSLT
by Anonymous Monk on Apr 07, 2009 at 09:56 UTC
    #Defining objects for XML,XSLT. my $parser = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); #Parsing both files my $source = $parser->parse_file($xmlfile); # '$style_doc' represents 'XML::LibXML::Document' object representing +an XSLT file. my $style_doc = $parser->parse_file($xslfile); my $stylesheet = $xslt->parse_stylesheet($style_doc); # 'Transform' returns a scalar that is the XSLT rendering of the 'XML: +:LibXML::Document' object using the desired output format(specified i +n the xsl:output tag in the stylesheet) my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to +_string(args1=> "$in{'args1'}",args2=> "$in{'args2'}",args3=> "$in{'a +rgs3'}",idx => "$in{'idx'}",newperiod => "$in{'newperiod'}")); # IN ABOVE CASE,I DN'T WANT TO HARDCODE 'ARGS' LIKE 3 ABOVE.I AM GETTI +NG TOTAL NUMBER OF PARAMETERS IN VARIABLE.SO IF ARGS CAN BE 5 IN SOME + CASE,ABOVE LINE SHOULD BE LIKE THIS.. # my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to +_string(args1=> "$in{'args1'}",args2=> "$in{'args2'}",args3=> "$in{'a +rgs3'}",args4=> "$in{'args4'}",args5=> "$in{'args5'}",idx => "$in{'id +x'}",newperiod => "$in{'newperiod'}")); print $stylesheet->output_string($results);
    Actually I want that it should check '$in' hash.It contains '$in{args<number>}'.It should pass these '$in{args<number>}' variables to XSL file.
      my $results = $stylesheet->transform($source, XML::LibXSLT::xpath_to_string(%in);

      this does the same as listing all the keys and values in %in by hand because the parameters of a function are evaluated in list context. In list context a hash is transformed into a list of key,value pairs. The same thing that happens when you do @a= %b