【问题标题】:XSLT pass regex-group() value to variable and formatting itXSLT 将 regex-group() 值传递给变量并对其进行格式化
【发布时间】:2018-01-28 11:34:01
【问题描述】:

我还没有找到有关如何执行此操作的明确示例。 我想将 2 个正则表达式组结果传递给分析字符串中的变量,其中一个应该从十六进制转换为十进制。例如,采用 regex-group(2)=2 和 regex-group(4)=30,regex-group(4) 应格式化为 0.30 两个值都传递给变量让我们说 $rg2 $rg4 然后计算 "($rg4* (100 格 60))+$rg2" "(0.30*(100 格 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"/>

它在 xmlspy 中返回“不是 x-path 语法的有效实例” 我不确定如何处理数字和数学以添加“0”。在 $rg4 前面就像一个字符串。

【问题讨论】:

  • 尝试使用以下语法:&lt;xsl:variable name="rg2" select="regex-group(2)"/&gt;。 --附言不要格式化您打算用作乘数的数字;格式化的数字是一个字符串。

标签: xml xslt xslt-2.0 saxon


【解决方案1】:

我没有设法了解您正在尝试做什么的全部细节,但是当您写作时

<xsl:value-of select="((0.$rg4)*(100 div 60))+$rg2"/>

那么我认为您误解了变量在 XSLT 中的工作方式。变量是可以在需要表达式的地方使用的命名值,它们不是可以在文本中的任何位置替换的字符串(也就是说,它们不是宏)。

如果 $rg4 是字符串“30”并且您想要值 0.30,那么您想要 0.$rg4 而不是 xs:decimal(concat("0.", $rg4))

【讨论】:

    【解决方案2】:

    绑定 XPath 表达式求值结果的语法(就像 regex-group(2) 的函数调用)很简单

    <xsl:variable name="rg2" select="regex-group(2)"/>
    

    &lt;xsl:variable name="rg2" as="xs:float"&gt;{regex-group(2)}&lt;/xsl:variable&gt; 可能在 XSLT 3.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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多