如何使用 XPATH XSLT 函数在 ${} 中查找字符串?
How to find the string within ${} using XPATH XSLT functions?
我知道一些 XPath 函数,例如 tokenize、substring-after、substring-before 等。使用 Xpath 函数从给定字符串中提取所有变量名的最佳方法是什么?
例如:
String:快速棕色 ${firstVar} 从 ${secondVar} 跳到 ${thirdVar}
输出:应给出 firstVar、secondVar、thirdVar,将使用 xsl:for-each 循环变量。
为此类变量解析字符串的最简便方法是使用 <xsl:analyze-string>
,尽管这是一个 XSLT 函数,而不是您请求的 XPath 函数。您只需要为变量定义一个模式,然后在 <xsl:matching-substring>
中获取结果,例如:
<xsl:analyze-string select="[[[xpath selector for your string]]]"
regex="$\{.+\}">
<xsl:matching-substring>
[[[ do your processing of the variable names here ]]]
</xsl:matching-substring>
</xsl:analyze-string>
您将在 W3C 规范文档中找到有关使用的更多信息:https://www.w3.org/TR/xslt20/#analyze-string
你可以这样做
tokenize($input, '(^.|\}).*?($\{|$)')
我不得不使用“^”。而不是“^”来绕过定界符不能为空字符串的规则,因此它不会匹配字符串开头的变量。您可以通过在字符串的开头连接 space 来解决这个问题,但可能有更聪明的方法。
我知道一些 XPath 函数,例如 tokenize、substring-after、substring-before 等。使用 Xpath 函数从给定字符串中提取所有变量名的最佳方法是什么?
例如:
String:快速棕色 ${firstVar} 从 ${secondVar} 跳到 ${thirdVar}
输出:应给出 firstVar、secondVar、thirdVar,将使用 xsl:for-each 循环变量。
为此类变量解析字符串的最简便方法是使用 <xsl:analyze-string>
,尽管这是一个 XSLT 函数,而不是您请求的 XPath 函数。您只需要为变量定义一个模式,然后在 <xsl:matching-substring>
中获取结果,例如:
<xsl:analyze-string select="[[[xpath selector for your string]]]"
regex="$\{.+\}">
<xsl:matching-substring>
[[[ do your processing of the variable names here ]]]
</xsl:matching-substring>
</xsl:analyze-string>
您将在 W3C 规范文档中找到有关使用的更多信息:https://www.w3.org/TR/xslt20/#analyze-string
你可以这样做
tokenize($input, '(^.|\}).*?($\{|$)')
我不得不使用“^”。而不是“^”来绕过定界符不能为空字符串的规则,因此它不会匹配字符串开头的变量。您可以通过在字符串的开头连接 space 来解决这个问题,但可能有更聪明的方法。