xslt - 匹配和替换 XSL 中的字符串
问题描述
我不是 XSL 专家。我需要匹配一个字符串并将其替换为 URL。字符串应该是 2 个字母 + 8 个数字的形式,例如 VA12345678。
这是我到目前为止所拥有的(我在同一个文件中有 XSL 和 HTML):
<xsl:variable name="fubar" select="'testing VA12345678'" />
<xsl:variable name="fubarNew">
<xsl:call-template name="replace">
<xsl:with-param name="in" select="$fubar" />
<xsl:with-param name="old" select="'regex will go here'" />
<xsl:with-param name="new" select="'<a href='/cs/page.asp?id=VA12345678'>VA12345678</a>" />
</xsl:call-template>
</xsl:variable>
<span><xsl:value-of select="$fubarNew" /></span>
fubarNew 应该看起来像testing VA12345678
VA12345678 链接到/cs/page.asp?id=VA12345678
.
如何在这里添加正则表达式?
解决方案
你的问题很不清楚。
如果 - 看起来 - 您想用 形式@@########
的锚元素替换每次出现的字符串(2 个大写 ASCII 字符后跟 8 位数字)<a href="/cs/page.asp?id=@@########">@@########</a>
,那么请考虑以下示例:
输入
<root>
<string>Lorem ipsum dolor sit amet, consectetuer ABC123 adipiscing elit.</string>
<string>Nam interdum ante quis VA12345678 erat pellentesque elementum. Ut molestie quam sit DT87654321 amet ligula.</string>
<string>In enim. XY55551234 Duis dapibus hendrerit quam.</string>
</root>
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="string">
<xsl:copy>
<xsl:analyze-string select="." regex="[A-Z]{{2}}\d{{8}}" >
<xsl:matching-substring>
<a href="/cs/page.asp?id={.}">
<xsl:value-of select="." />
</a>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<string>Lorem ipsum dolor sit amet, consectetuer ABC123 adipiscing elit.</string>
<string>Nam interdum ante quis <a href="/cs/page.asp?id=VA12345678">VA12345678</a> erat pellentesque elementum. Ut molestie quam sit <a href="/cs/page.asp?id=DT87654321">DT87654321</a> amet ligula.</string>
<string>In enim. <a href="/cs/page.asp?id=XY55551234">XY55551234</a> Duis dapibus hendrerit quam.</string>
</root>
推荐阅读
- javascript - 为什么我们应该在 return 语句中使用 {}
- sql - PL/SQL 你可以 INSERT INTO (SELECT GROUP BY) 吗?
- jupyter-notebook - RuntimeError: 你不应该在 `Formula` 上调用 `__bool__` / `__nonzero__`,
- python - 使用 Sci-Kit 的 Count Vectorizer 转换输入以仅匹配词汇表中的确切单词
- android - android中的同步改造请求
- r - 亚美尼亚语的 RMarkdown 和针织
- c# - C#在具有容差的图像中查找图像
- php - `preg_split` 在什么情况下可能返回 `false`
- python - Python:如何从请求标头中提取值?
- python-3.x - 运行 keras model.fit_generator 后需要帮助纠正错误