xsl-list
[Top] [All Lists]

Re: [xsl] passing parameters to XSL,what if no value in some cases

2009-03-06 08:40:38
2009/3/6 himanshu padmanabhi <himanshu(_dot_)padmanabhi(_at_)gmail(_dot_)com>:
First time,code will go to 'else' part,it again returns to this form
and 'if' part executes.

<CODE>
        my $parser = XML::LibXML->new();
        my $xslt = XML::LibXSLT->new();

        my $source = $parser->parse_file($xmlfile);
        my $style_doc = $parser->parse_file($xslfile);

        my $stylesheet = $xslt->parse_stylesheet($style_doc);
        if($in{'flag'} eq "2") {
            my $results = $stylesheet->transform($source,
XML::LibXSLT::xpath_to_string(args => "$in{'args'}",val => "1",new =>
"$in{'new'}"));
            print $stylesheet->output_string($results);
        }else{
            my $results = $stylesheet->transform($source,
XML::LibXSLT::xpath_to_string(args => "",val => "3",new =>
"$in{'new'}"));
            print $stylesheet->output_string($results);
        }
</CODE>

1.In my 'else' above,I don't want to pass 'args'(can I do it?),

2.How can I check in my xsl file whether it is passed or not probably
using some 'if' statement or something like this?
<CODE>
   <xsl:choose>
   <xsl:when test="args_is_given">
        <some code>
   </xsl:when>
   </xsl:choose>
</CODE>

If you were to choose to use a sensible default parameter as suggested
by Michael Kay
then you could rewrite that perl to be a little simpler and avoid
passing the redundant arg. Something like:

my $parser = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();

my $source = $parser->parse_file($xmlfile);
my $style_doc = $parser->parse_file($xslfile);

my $stylesheet = $xslt->parse_stylesheet($style_doc);

# The quotes around your args are redundant unless
# the values are actually objects not scalar data.
my %params = (
  val => XML::LibXSLT::xpath_to_string(1),
  new =>  XML::LibXSLT::xpath_to_string($in{'new'})
);

# I changed this to a numeric comparison
# (this may be wrong in your case).
$params{'args'} = XML::LibXST::xpath_to_string($in{'args'}) if $in{'flag'} == 2;

my $results = $stylesheet->transform($source, %params);
print $stylesheet->output_string($results);


I don't tend to write that exact code myself. I usually have a function to
set up the arguments but your mileage may vary...

cheers

nic

-- 
Nic Gibson
Director, Corbas Consulting
Editorial and Technical Consultancy
http://www.corbas.co.uk/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--