[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: Re: [xsl] passing parameters to XSL,what if no value in some cases
From: Nic Gibson <nicg@xxxxxxxxxx>
Date: Fri, 6 Mar 2009 13:40:06 +0000

2009/3/6 himanshu padmanabhi <himanshu.padmanabhi@xxxxxxxxx>:
> First time,code will go to 'else' part,it again returns to this form
> and 'if' part executes.
>
> <CODE>
> B  B  B  B  my $parser = XML::LibXML->new();
> B B B B B B B  my $xslt = XML::LibXSLT->new();
>
> B B B B B B B  my $source = $parser->parse_file($xmlfile);
> B B B B B B B  my $style_doc = $parser->parse_file($xslfile);
>
> B B B B B B B  my $stylesheet = $xslt->parse_stylesheet($style_doc);
> B B B B B B B  if($in{'flag'} eq "2") {
> B B B B B B B B B B B  my $results = $stylesheet->transform($source,
> XML::LibXSLT::xpath_to_string(args => "$in{'args'}",val => "1",new =>
> "$in{'new'}"));
> B B B B B B B B B B B  print $stylesheet->output_string($results);
> B B B B B B B  }else{
> B B B B B B B B B B B  my $results = $stylesheet->transform($source,
> XML::LibXSLT::xpath_to_string(args => "",val => "3",new =>
> "$in{'new'}"));
> B B B B B B B B B B B  print $stylesheet->output_string($results);
> B B B B B B B  }
> </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>
> B  B <xsl:choose>
> B  B <xsl:when test="args_is_given">
> B  B  B  B  <some code>
> B  B </xsl:when>
> B  B </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/


Current Thread
Keywords
xsl