[xsl] xsl:analyze-string explanation needed

Subject: [xsl] xsl:analyze-string explanation needed
From: cknell@xxxxxxxxxx
Date: Thu, 20 Jul 2006 09:20:22 -0400
I'm running a stylesheet to perform a statistical analysis of a series of runs on an Ant build.xml file. One of the tasks is to extract the timing information from a number of elements in the log file. The timing information is presented in an attribute in this format: "3 minutes 57 seconds".

I pass this string as a parameter to three named templates to convert the time information into the same units, viz., seconds. Below is one of these templates. The problem I have is, apparently, a faulty mental model of the functioning of  xsl:analyze-string.

Looking at it, I see the general form as:

Compare the string to the regular expression.
If there is a match, output something.
If there is no match, output something else.

Given the input "3 minutes 57 seconds", I expect the output of this template to be "180". Instead, the output is "1800", that is to say, a concatenation of the values produced in each of the two branches, rather than one or the other.

Can someone explain the operation of this element and how I can get the output of either one OR the other branch, but not both?

Thanks.

<xsl:template name="extract-minutes-as-seconds">
  <xsl:param name="time" />
  <xsl:analyze-string select="$time" regex="([0-9]+ minutes)">
  	<xsl:matching-substring>
  		<xsl:value-of select="60 * xs:integer((substring-before(regex-group(1),' minutes')))" />
  	</xsl:matching-substring>
  	<xsl:non-matching-substring>
  		<xsl:value-of select="'0'" />
  	</xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

-- 
Charles Knell
cknell@xxxxxxxxxx - email

Current Thread