這裏是匹配(在XSLT 2.0使用匹配()函數與真正的正則表達式作爲pattern
參數)的正確XSLT 1.0方式:
匹配的元件的名稱包含'line'
:
<xsl:template match="*[contains(name(), 'line')]">
<!-- Whatever processing is necessary -->
</xsl:template>
匹配的元件,其名稱結束在'line'
:
<xsl:template match="*[substring(name(), string-length() -3) = 'line']">
<!-- Whatever processing is necessary -->
</xsl:template>
@Tomalak提供了另一種查找以給定字符串結尾的名稱的XSLT 1.0方式。他的解決方案使用了一個特殊的角色,保證不會以任何名義出現。我的解決方案可以應用於查找是否有任何字符串(不僅是一個元素的名稱)以另一個給定的字符串結尾。
在XSLT 2中。X:
使用:matches(name(), '.*line$')
,匹配以字符串"line"
這種轉變結尾的名稱:
當泰斯XML文檔施加:
<greeting>
<aaa>Hello</aaa>
<bblineb>Good</bblineb>
<ccc>Excellent</ccc>
<dddline>Line</dddline>
</greeting>
複製到輸出只元件,其名稱與字符串"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"
:
<bblineb>Good</bblineb>
<dddline>Line</dddline>
下一次,請你只閱讀文本框,在那裏說旁邊的複選框*」如何格式化:由4個空格縮進代碼「*。那麼你不必使用'<'等...... – 2010-06-20 22:34:42
好問題(+1)。看到我的答案,這個答案在這個時候包含你的兩個相關問題的唯一完全正確的解決方案。 :) – 2010-06-21 02:50:53