XSLT 2.0 中简洁的正则表达式提取功能 [英] Concise regex extract function in XSLT 2.0

查看:20
本文介绍了XSLT 2.0 中简洁的正则表达式提取功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽我所能从 XSLT 2.0 文档中看到,或者至少从我在这些地方读到的:

As best I can see from the XSLT 2.0 docs, or at least from what I read at these places:

撒克逊分析字符串函数

XML.com

使用正则表达式从某些输入中提取特定组的唯一方法是使用analyze-string().相比之下,replace() 函数非常简洁,是否有同样简洁的提取方法?

the only way to use a regular expression to extract a specific group from some input is to use analyze-string(). Given that the replace() function is very concise by comparison, is there nothing equally concise for extraction?

例如,给定输入:ABCD: 123, DEFGH",我只想提取123"并将其存储在变量中.请注意,输入可能更复杂,因此我确实想利用正则表达式和分组.

For example, given the input: "ABCD: 123, DEFGH", I would like to extract just "123" and store it in a variable. Note the input can be more complex so I do want to take advantage of regular expressions and grouping.

tokenize() 不是答案,IMO,因为它似乎更适合输入具有重复分隔符的情况.

tokenize() is not the answer, IMO, because it seems more suited to when the input has repeating delimiters.

我正在寻找一个与 replace() 一样简洁的函数,并且可能像这样工作:

I was searching for a function that is as concise as replace() and might work like this:

<xsl:variable name="num" select="extract('.', '(.+):\s*(\d+), (.+)', '$2')"/>

但是我找不到.看来我必须像下面那样使用analyze-string(),但它太长了:

but I cannot find one. It seems I must use analyze-string() like I do below but it is just so lengthy:

<xsl:variable name="num">
  <xsl:analyze-string select="." regex="(.+):\s*(\d+), (.+)">

    <xsl:matching-substring>
      <xsl:value-of select="regex-group(2)"/>
    </xsl:matching-substring>

  </xsl:analyze-string>
</xsl:variable>

真的有必要使用 analyze-string() 还是我遗漏了什么?

Is it actually necessary to use analyze-string() or am I missing something?

推荐答案

replace() 正是您正在寻找的.matches() 是用来比较的函数,replace() 是用来提取的.如果您在示例中用 replace 替换 extract ,它就可以正常工作XSLT 转换不会更改您的输入.它将您的输入转换为输出.您的输入将完好无损.

The replace() is exactly that you are looking for. matches() is the function used for comparison, and replace() for extraction. It just works fine if you substitute extract with replace in your example The XSLT transformation doesn't change your input. It transforms your input into an output. Your input will be intact.

这篇关于XSLT 2.0 中简洁的正则表达式提取功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆