xsl:template 中的正则表达式匹配属性 [英] Regular Expressions in xsl:template match attribute
问题描述
我只想知道是否可以在 xsl:template
元素的 match
属性中使用正则表达式.例如,假设我有以下 XML 文档:
I just want to know if it's possible to use regular expressions in the match
attribute of the xsl:template
element.
For example, suppose I have the follow XML document:
<greeting>
<aaa>Hello</aaa>
<bbb>Good</bbb>
<ccc>Excellent</ccc>
<dddline>Line</dddline>
</greeting>
现在用XSLT来转换上面的文件:
Now the XSLT to transform the above document:
<xsl:stylesheet>
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="matches(node-name(*),'line')">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
当我尝试在 xsl:template 的
match
属性中使用语法 matches(node-name(*),'line$')
元素,它检索错误消息.我可以在 match
属性中使用正则表达式吗?
When I try to use the syntax matches(node-name(*),'line$')
in the match
attribute of the xsl:template
element, it retrieves a error message. Can I use regular expressions in the match
attribute?
非常感谢
推荐答案
这是正确的 XSLT 1.0 匹配方式(在 XSLT 2.0 中使用带有真正 RegEx 的matches() 函数作为 <代码>模式代码>参数):
Here is the correct XSLT 1.0 way of matching (in XSLT 2.0 use the matches() function with a real RegEx as the pattern
argument):
匹配名称包含'line'
的元素:
Matching an element whose name contains 'line'
:
<xsl:template match="*[contains(name(), 'line')]">
<!-- Whatever processing is necessary -->
</xsl:template>
匹配名称以'line'
结尾的元素:
Matching an element whose name ends in 'line'
:
<xsl:template match="*[substring(name(), string-length() -3) = 'line']">
<!-- Whatever processing is necessary -->
</xsl:template>
@Tomalak 提供了另一种 XSLT 1.0 方法来查找以给定字符串结尾的名称.他的解决方案使用了一个特殊字符,保证不会出现在任何名称中.我的解决方案可用于查找是否有任何字符串(不仅仅是元素的名称)以另一个给定的字符串结尾.
@Tomalak provided another XSLT 1.0 way of finding names that end with a given string. His solution uses a special character that is guaranteed not to be ever present in any name. My solution can be applied to find if any string (not only a name of an element) ends with another given string.
在 XSLT 2.x 中:
使用: matches(name(), '.*line$')
匹配以字符串"line"
结尾的名字
Use: matches(name(), '.*line$')
to match names that end with the string "line"
这种转变:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*[matches(name(), '.*line$')]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*[not(matches(name(), '.*line$'))]">
<xsl:apply-templates select="node()[not(self::text())]"/>
</xsl:template>
</xsl:stylesheet>
应用于 XML 文档时:
<greeting>
<aaa>Hello</aaa>
<bblineb>Good</bblineb>
<ccc>Excellent</ccc>
<dddline>Line</dddline>
</greeting>
仅将名称以字符串 "line"
结尾的元素复制到输出:
Copies to the output only the element, whose name ends with the string "line"
:
<dddline>Line</dddline>
虽然这个转换(使用 matches(name(), '.*line')
):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*[matches(name(), '.*line')]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*[not(matches(name(), '.*line'))]">
<xsl:apply-templates select="node()[not(self::text())]"/>
</xsl:template>
</xsl:stylesheet>
将名称包含字符串 "line"
的所有元素复制到输出:
copies to the output all elements, whose names contain the string "line"
:
<bblineb>Good</bblineb>
<dddline>Line</dddline>
这篇关于xsl:template 中的正则表达式匹配属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!