在如何做到这一点上,我没有找到明确的例子。我要把两个正则组的结果传递给变量内的分析字符串,一个应该从十六进制转换成十进制。例如,以regex-group(2)=2和regex-group(4)=30为例,regex-group(4)应该被格式化为0.30,这两个值都传递给变量$rg2 $rg4,然后计算"($rg4*(100 div 60))+$rg2“(0.30*(100 div 60))+2"=2.5。如果是rg4=0.38,那么最终结果将是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>怎么做?可能有一条干净快捷的路。我不知道您是否必须使用变量,或者在xslt中是否存在这方面的范围和转换值问题。
编辑:我想我问这个问题是因为我最初尝试过这样做:
<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"/>它返回了xml间谍中的“非有效的x路径语法实例”--我不确定如何处理数字和数学以添加“0”。就像一个字符串一样在$rg4面前。
发布于 2017-08-20 07:51:14
绑定计算结果的语法-- XPath表达式(类似于函数调用,regex-group(2)是)--是简单的
<xsl:variable name="rg2" select="regex-group(2)"/><xsl:variable name="rg2" as="xs:float">{regex-group(2)}</xsl:variable>可以在XSLT3.0中使用expand-text="yes"集。
通常,如果您有一个字符串(如regex-group()返回的字符串),并且需要若干特定类型的字符串,则调用构造函数。
<xsl:variable name="rg2" select="xs:decimal(regex-group(2))"/>对于算术计算,($rg4*(100 div 60))+$rg2需要两个数值,而format-number会给出一个字符串,所以我想您更愿意定义
<xsl:variable name="rg4" select="xs:decimal(regex-group(4)) div 100"/>发布于 2017-08-20 22:51:58
我没有完全了解你想做的事情,但是当你写的时候
<xsl:value-of select="((0.$rg4)*(100 div 60))+$rg2"/>那么,我认为您误解了XSLT中变量的工作方式。变量是命名值,可以在表达式的预期位置使用,它们不是可以在文本中任何地方替换的字符串(也就是说,它们不是宏)。
如果$rg4是字符串"30“,并且要值0.30,则需要xs:decimal(concat("0.", $rg4))而不是0.$rg4
https://stackoverflow.com/questions/45779379
复制相似问题