xml - XSLT pass regex-group() value to variable and formatting it -
i have not find clear example on how this. want pass 2 regex-group result variable inside analyse-string 1 should tranformed hexadecimal decimal. example take regex-group(2)=2 , regex-group(4)=30, regex-group(4) should formated 0.30 both value passed variable lets $rg2 $rg4 calculating "($rg4*(100 div 60))+$rg2" "(0.30*(100 div 60))+2"=2.5 . if rg4=0.38 final result 2.6333333333333333
<xsl:analyze-string select="sbtime/@stmerid" regex="([hm]{{1}})([0-9]{{1,2}})([ew]{{1}})([0-9]{{0,2}})"> <xsl:matching-substring> <xsl:choose> <xsl:when test="regex-group(1) = 'm'"> <xsl:choose> <xsl:when test="regex-group(3) = 'e'"> <xsl:text>-</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>+</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:choose> <xsl:when test="regex-group(4) != ''"> <xsl:text>:</xsl:text> <xsl:variable name="rg2" as="xs:float">{regex-group(2)}</xsl:variable> <xsl:variable name="rg4" as="xs:float">fn:format-number({regex-group(4)},'#.##')</xsl:variable> <xsl:value-of select="($rg4*(100 div 60))+$rg2"/> </xsl:when> <xsl:otherwise> <xsl:number value="regex-group(2)" format="1"/> </xsl:otherwise> </xsl:choose> <xsl:number value="regex-group(2)" format="1"/> </xsl:when> <xsl:otherwise> <xsl:choose> <xsl:when test="regex-group(3) = 'e'"> <xsl:text>-</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>+</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:if test="regex-group(1) = 'm'"><xsl:text>00:</xsl:text></xsl:if> <xsl:number value="regex-group(2)" format="1"/> <xsl:choose> <xsl:when test="regex-group(4) != ''"> <xsl:text>:</xsl:text> <xsl:number value="regex-group(4)" format="1"/> </xsl:when> <xsl:otherwise> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </xsl:matching-substring> </xsl:analyze-string>
how ? there clean , fast way. don't know if have use variable or if there problem of scope , cast values in xslt.
edit: think ask question because tried this:
<xsl:variable name="rg2" select="regex-group(2)"/> <xsl:variable name="rg4" select="regex-group(4)"/> <xsl:value-of select="((0.$rg4)*(100 div 60))+$rg2"/>
and returned "not valid instance of x-path grammar" in xmlspy not sure on how handle number , math add "0." in front of $rg4 string.
the syntax bind result of evaluation xpath expression (like function call regex-group(2)
is) simply
<xsl:variable name="rg2" select="regex-group(2)"/>
<xsl:variable name="rg2" as="xs:float">{regex-group(2)}</xsl:variable>
might in xslt 3.0 expand-text="yes"
set.
in general, if have string (like returned regex-group()
) , need number of type call constructor e.g.
<xsl:variable name="rg2" select="xs:decimal(regex-group(2))"/>
for arithmetic computation ($rg4*(100 div 60))+$rg2
need 2 numeric values while format-number
give string guess rather want define
<xsl:variable name="rg4" select="xs:decimal(regex-group(4)) div 100"/>
Comments
Post a Comment