xpath : test if node is the only child other than specified ok elements



I want to write an xsl template that checks to see if a given node is the only child node, other than certain specified elements :


in this example, <target/> will be changed to <hit/> as it is the only <target/> node, and only <ok/> nodes precede it



<root>
<!-- this is ok, the ok nodes are at the top, followed by only 1 target -->
<mynode>
<ok1/>
<ok2/>
<target/>
</mynode>

<!-- will fail, bad element before target -->
<mynode>
<ok1/>
<ok2/>
<bad/>
<target/>
</mynode>

<!-- no match, multiple target nodes -->
<mynode>
<ok1/>
<ok2/>
<target/>
<target/>
</mynode>
</root>


I was using this xpath :



<xsl:template match="target[not(following-sibling::*)]
[not(preceding-sibling::target)]
[not(preceding-sibling::*[starts-with(name(), 'bad' or 'hello')])]
">
<hit>
<xsl:apply-templates/>
</hit>
</xsl:template>


In that last predicate, do I have to indicate specifically any node I don't want? Can I something like



not(preceding-sibling::*[not(starts-with(name(), 'ok'))])


thanks


No comments:

Post a Comment