Match a node if it has a certain attribute and its

2019-08-02 18:26发布

OK, I have a finding aid structure like so:

<c01 level="file">

<c02 level="file"></c02>

</c01>

For my XSLT template, I'd like to be able to match only nodes with the attribute of level="file" that have a parent also with the attribute of level="file" so that I can enact some specific formatting on the child.

Normally to match level="file" I just use the following:

<xsl:template match="*[@level="file"]">

However, for the purposes of formatting (indentation, etc), I need the node that is a child of a node with the same attribute to be treated differently than its parent. So something like:

<xsl:template match="*[@level="file"] and parent::[@level="file"]">

Any ideas? I hope this makes sense. Thanks!

标签: xml xslt xpath
3条回答
别忘想泡老子
2楼-- · 2019-08-02 19:17

Use:

<xsl:template match="*[@level='file' ]/*[@level='file']">

This template matches any element the string value of whose level attribute is "file" and that is a child of an element the string value of whose level attribute is "file"

Do note: No .. or parent:: axis or // are used and this is probably the simplest and most precise match pattern.

查看更多
Ridiculous、
3楼-- · 2019-08-02 19:22

Match on any node whose parent has the same @level attribute as myself, and my @level attribute = 'file'

<xsl:template match="*[../@level=./@level][./@level='file']">
 ...
</xsl:template>
查看更多
甜甜的少女心
4楼-- · 2019-08-02 19:23

I think you're looking for something like

<xsl:apply-templates select="//*[@level='file' and parent::*/@level='file']" />

That is, select any element, that has a "level" attribute of "file", and also has a parent element that has a "level" attribute of "file". You'd then be able to have your template match on it. I'm not sure if the match syntax on templates is sophisticated enough for the kind of complexity you're looking for; I think the approach I generally take is to select the nodes that I'm looking for, and only use the match on templates for very simple differences. What's clearest might depend on what exactly the rest of your transform looks like.

I'm not sure that I fully understood your question, though.

查看更多
登录 后发表回答