Saxon XSLT2.0 从字符串中提取数字

Saxon XSLT2.0 Extracting Numbers from the String

我正在尝试使用 Xslt2.0 从字符串中提取整数 例如,考虑字符串 "designa80000dd5424d" 并且我需要字符串中的两个整数,即“8000”和“5424”

我试过如下使用翻译功能

select="translate($term,translate($term, '0123456789', ''), '')"

但它结合了两个整数并给出了输出“80005424” 我需要一些东西把它们分开

您可以尝试使用 tokenize 和任何非数字序列作为分隔符,即使用 XPath 3.0 tokenize('designa80000dd5424d', '[^0-9]+')[normalize-space()]!number() 或在 XSLT/XPath 2.0 中作为 for $t in tokenize('designa80000dd5424d', '[^0-9]+')[normalize-space()] return number($t) 或者您可以使用 xsl:analyze-string (XSLT 2.0) 或 analyze-string 函数(XSLT/XPath 3.0,但适用于 Saxon 9.7 HE)。[​​=15=]

I tried using translate function as below

select="translate($term,translate($term, '0123456789', ''), '')"

But it combines both the numbers and gives the output as "80005424" i need something which separates them

我。这是一个完整的 XSLT 1.0 解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

  <xsl:template match="/*">
    <xsl:variable name="vSpaces">
      <xsl:call-template name="makeSpaces"/>
    </xsl:variable>

    <xsl:variable name="vtheNumbers" 
         select="normalize-space(translate(., translate(.,'0123456789',''), $vSpaces))"/>

    <xsl:call-template name="tokenize">
      <xsl:with-param name="pStr" select="$vtheNumbers"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="tokenize">
    <xsl:param name="pStr"/>
    <xsl:param name="pInd" select="1"/>

    <xsl:if test="string-length($pStr)">
      <xsl:value-of select=
           "concat($pInd, ': ',substring-before(concat($pStr, ' '), ' '), '&#xA;')"/>

      <xsl:call-template name="tokenize">
        <xsl:with-param name="pStr" select="substring-after($pStr, ' ')"/>
        <xsl:with-param name="pInd" select="$pInd +1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name="makeSpaces">
    <xsl:param name="pLen" select="string-length(.)"/>

    <xsl:choose>
      <xsl:when test="$pLen = 1">
        <xsl:value-of select="' '"/>
      </xsl:when>
      <xsl:when test="$pLen > 1">
        <xsl:variable name="vHalfLen" select="floor($pLen div 2)"/>

        <xsl:call-template name="makeSpaces">
          <xsl:with-param name="pLen" select="$vHalfLen"/>
        </xsl:call-template>
        <xsl:call-template name="makeSpaces">
          <xsl:with-param name="pLen" select="$pLen -$vHalfLen"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<t>designa80000dd5424dan1733g122</t>

产生了想要的、正确的结果:

1: 80000
2: 5424
3: 1733
4: 122

注意:

外层的最后一个参数translate()是一个与输入字符串字符数相同的字符串,每个字符都是一个space。


二. XPath 2.0 更短更简单

这个 XPath 2.0 表达式在求值时会产生所需的数字序列:

tokenize(., '[^\d]+')[.]

这是一个基于XSLT的验证:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/*">
    <xsl:variable name="vNumbers" 
    select="tokenize(., '[^\d]+')[.]"/>

    <xsl:for-each select="$vNumbers">
      <xsl:value-of select="concat(position(), ': ', ., '&#xA;')"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于同一 XML 文档时:

<t>designa80000dd5424dan1733g122</t>

产生同样正确的结果:

1: 80000
2: 5424
3: 1733
4: 122