I have an XML structure representing a logical derivation, and it looks like this:
<derivation>
<step name="foo">
<derive>[...]</derive>
<by>[...]</by>
</step>
<step name="bar">
<derive>[...]</derive>
<by>
<from name="foo"/>, conjunction elimination
</by>
</step>
<step name="baz">
<derive>[...]</derive>
<by>
<from name="bar"/>, existential quantification
</by>
</step>
[...]
</derivation>
Each <step> in the derivation has a number — for instance, that with name="foo" would be number 1, that with name="bar" would be number 2, that with name="baz" would be number 3, and so on. When inside the <step>, I can find the number with <xsl:number/>. Good so far.
Now, where an element <from name="bar"/> occurs, I want it to be replaced by the number of the <step> element with name="bar". There are three subproblems to solve here:
- Find the most recent ancestor of the
<from>element that is a<derivation>. - From that, find the first child that is a
<step>element withname="bar". In the above instance, this would find the second child of the<derivation>. - Identify the number of that element. In the above, that would be
2.
Can someone tie together solutions to these subproblems to satisfy my requirement?
This transformation:
when applied on the provided XML document:
produces the wanted, correct result: