XSLT - 检查元素字符串中是否存在模式 [英] XSLT - Check if pattern exists in an element string

查看:25
本文介绍了XSLT - 检查元素字符串中是否存在模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下元素作为较大 XML 的一部分

I have the following element as part of a larger XML

<MT N="NonEnglishAbstract" V="[DE] Deutsch Abstract text [FR] French Abstract text"/>

我需要对 @V 属性中的值进行一些格式化,前提是它包含诸如 [DE]、[FR] 或方括号内代表国家/地区代码的任意两个大写字母.

I need to do some formatting of the value in @V attribute, only if it contains anything like [DE], [FR] or any two capital letters representing a country code within square brackets.

如果不存在这样的模式,我只需要简单地写出@V的值,不进行任何格式化.

If no such pattern exist, I need to simply write the value of @V without any formatting.

我可以使用 XSLT 2.0 解决方案

I can use an XSLT 2.0 solution

我希望我可以使用matches()函数之类的

I was hoping that I could use the matches() function something like

<xsl:choose>
<xsl:when test="matches(@V,'\[([A-Z]{{2}})\]([^\[]+'">
//Do something
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@V"/>
</xsl:otherwise>
</xsl:choose>

推荐答案

我认为您只需要:

matches(@V,'\[[A-Z][A-Z]\]')

你不必匹配整个字符串来获得 true() ...我告诉我的学生尽可能写短的正则表达式.

You don't have to match the entire string to get a true() ... I tell my students to write as short a reg-ex as possible.

这篇关于XSLT - 检查元素字符串中是否存在模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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