gpt4 book ai didi

regex - XSLT,使用正则表达式从 xml 中提取子字符串

转载 作者:行者123 更新时间:2023-12-01 11:30:20 24 4
gpt4 key购买 nike

我正在尝试在 SVN 日志上应用 XSLT,我需要从提交消息中提取错误编号。我在 msg 上应用这个正则表达式,但没有得到任何返回。我在 XSLT 中缺少什么?先感谢您以下是我从 SVN 获得的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<log>
<logentry revision="265">
<author>dre</author>
<date>2015-04-13T02:35:25.246150Z</date>
<msg>modified code</msg>
</logentry>
<logentry revision="73283">
<author>john</author>
<date>2015-04-13T14:10:20.987159Z</date>
<msg>fixed bug DESK-1868</msg>
</logentry>
<logentry revision="73290">
<author>ron</author>
<date>2015-04-13T14:24:57.475711Z</date>
<msg>WEBAPP-1868 Fix for pallete list and settings dialog Selected Tab Index</msg>
</logentry>
</log>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>SVN Issues</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">ver</th>
<th style="text-align:left">author</th>
<th style="text-align:left">date</th>
<th style="text-align:left">ticket</th>
</tr>
<xsl:for-each select="log/logentry">
<tr>
<td><xsl:value-of select="@revision"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="date"/></td>
<td>

<xsl:variable name="messageValue" select="msg"/>
<xsl:analyze-string select="$messageValue"
regex="(DESK|TRS|PEK|WEBAPP)-\d{4}$">
<xsl:matching-substring>
<bug><xsl:value-of select="regex-group(1)"/></bug>
</xsl:matching-substring>
</xsl:analyze-string>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

最佳答案

  1. http://www.w3.org/TR/xslt20/#analyze-string

    Note: Because the regex attribute is an attribute value template, curly brackets within the regular expression must be doubled. For example, to match a sequence of one to five characters, write regex=".{{1,5}}". For regular expressions containing many curly brackets it may be more convenient to use a notation such as regex="{'[0-9]{1,5}[a-z]{3}[0-9]{1,2}'}", or to use a variable.

  2. 您不想在表达式末尾使用 $ 将表达式锚定到行尾。否则,正则表达式只会在消息以问题 ID 结尾时匹配。

使用此正则表达式来捕获整个错误编号:

regex="((DESK|TRS|PEK|WEBAPP)-\d{{4}})"

关于regex - XSLT,使用正则表达式从 xml 中提取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32661801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com