RE: [xsl] Count number of specific child templates

Subject: RE: [xsl] Count number of specific child templates
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 7 Apr 2009 09:15:49 +0100
What should the result be if <parameters> contains 2 <service1> children and
5 <service2> children?

If the answer is 7, then perhaps you want

count(*[starts-with(local-name(), 'service')])

Generally it looks like bad XML design to have some semantic significance
implied by the fact that <service1> and <service2> have similar names.
Better design would be to use <service nr="1"> and <service nr="2">.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: himanshu padmanabhi [mailto:himanshu.padmanabhi@xxxxxxxxx] 
> Sent: 07 April 2009 08:44
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Count number of specific child templates
> 
> Case 1
> 
> <?xml version='1.0'?>
> <?xml-stylesheet href="service1.xsl" type="text/xsl"?>
> 
> <parameters>
>     <service1>
>           <label>first parameter</label>
>           <desc>specify machine name</desc>
>     </service1>
>     <service1>
>           <label>second parameter</label>
>           <desc>specify machine IP</desc>
>     </service1>
>     <cmd> -p args1 -t args2 </cmd>
> </parameters>
> 
> XSL variable should give me value 2 as there are 2 'service1' 
> templates.
> 
> Case 2
> 
> <?xml version='1.0'?>
> <?xml-stylesheet href="service1.xsl" type="text/xsl"?>
> 
> <parameters>
>     <service2>
>           <label>first parameter</label>
>           <desc>specify machine name</desc>
>     </service2>
>     <service2>
>           <label>second parameter</label>
>           <desc>specify machine IP</desc>
>     </service2>
>     <service2>
>           <label>third parameter</label>
>           <desc>specify third param. as Y or N</desc>
>     </service2>
>     <service2>
>           <label>forth parameter</label>
>           <desc>specify forth param between 1 to 100</desc>
>     </service2>
>      <cmd> --name args1 --value args2 --decision args3 
> --percentage args4</cmd> </parameters>
> 
> XSL variable should give me value 4 as there are 4 'service2' 
> templates.
> 
> I am using perl:libxslt processor and using XSL version 1.0.I 
> want xsl command for giving desired outputs in both cases 
> above, So,how can I count total number of "service<no.>" 
> templates above using XSL variable(or any other xsl feature)?
> So that I will get total number of arguments for each service.
> ---------------------------------
> Thanks and Regards,
> Himanshu Padmanabhi

Current Thread