0

我有一个看起来像的 xml 文档

<a>
<somenode att="1"></somenode>
</a>
<a01>
<apple att="2"></apple>
<somenode att="1"></somenode>
</a01>

当节点名称为“a”或a后跟一个数字(a0)和a后跟两个数字(a01)时,我想做的是匹配,关于我如何做到这一点的任何想法?

到目前为止,我有以下内容

<xsl:apply-templates select="node()[starts-with(name(),'a')]">

但这也会选择苹果,如何进行多个匹配,例如和 OR/AND ?

4

1 回答 1

4

In XSLT 2.0 this would be pretty simple:

<xsl:apply-templates select="node()[matches(name(),'^a\d?\d?$')]">

In XSLT 1.0, regex isn't available, so it's a little trickier, but the following should work:

<xsl:apply-templates select="node()[starts-with(name(), 'a') and
                                    string-length(name()) &lt;= 3 and
                                    translate(name(), '0123456789', '') = 'a']" />

The three parts joined by "and" here ensure that:

  • The name starts with "a"
  • The name is 3 characters or less
  • All that remains when digits are removed from the name is the string "a" (meaning that any characters besides that single "a" were digits).
于 2013-02-08T15:59:46.063 回答