Letterpart
asked on
using XSLT 2.0 to get an attribute value from nearest ancestor, of a specific element name, and adding to current node
Given this simplified input data:
For each section element in the file I want to add an attribute (prevType) that is populated with the type attribute of the section element that precedes it - it does not matter about the hierarchy of the section elements, I always want the previous one.
My sample data only shows the section elements but there will be a lot data in between the sections in the 'real' file.
I think I need to examine and get what I need from the ancestor axis but am not sure how to put that into an XSLT command. Can anyone assist please?
<data>
<section type="AAA">
<section type="CCC">..</section>
<section type="CCC">..</section>
<section type="CCC">..</section>
</section>
<section type="AAA">
<section type="CCC">..</section>
<section type="CCC">..</section>
<section type="CCC">..</section>
</section>
<section type="AAA">
<section type="BBB">
<section type="CCC">..</section>
<section type="CCC">..</section>
<section type="CCC">..</section>
</section>
</section>
<section type="CCC">..</section>
<section type="CCC">..</section>
<section type="CCC">..</section>
<section type="BBB">
<section type="CCC">..</section>
<section type="CCC">..</section>
<section type="CCC">..</section>
</section>
</data>
For each section element in the file I want to add an attribute (prevType) that is populated with the type attribute of the section element that precedes it - it does not matter about the hierarchy of the section elements, I always want the previous one.
My sample data only shows the section elements but there will be a lot data in between the sections in the 'real' file.
<data>
<section type="AAA">
<section type="CCC" prevType="AAA">..</section>
<section type="CCC" prevType="CCC">..</section>
<section type="CCC" prevType="CCC">..</section>
</section>
<section type="AAA" prevtype="CCC">
<section type="CCC" prevType="AAA">..</section>
<section type="CCC" prevType="CCC">..</section>
<section type="CCC" prevType="CCC">..</section>
</section>
<section type="AAA" prevType="CCC">
<section type="BBB" prevType="AAA">
<section type="CCC" prevType="BBB">..</section>
<section type="CCC" prevType="CCC">..</section>
<section type="CCC" prevType="CCC">..</section>
</section>
</section>
<section type="CCC" prevType="CCC">..</section>
<section type="CCC" prevType="CCC">..</section>
<section type="CCC" prevType="CCC">..</section>
<section type="BBB" prevType="CCC">
<section type="CCC" prevType="BBB">..</section>
<section type="CCC" prevType="CCC">..</section>
<section type="CCC" prevType="CCC">..</section>
</section>
</data>
I think I need to examine and get what I need from the ancestor axis but am not sure how to put that into an XSLT command. Can anyone assist please?
ASKER
Yes, you're correct Geert - it's the trickier one that I need.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's great - working fine - thanks very much Geert.
welcome, fun challenge actually
preceding will give you the last section that closed
using preceding:: will give
<section type="AAA">
<section type="CCC" prevType="AAA">..</section
<section type="CCC" prevType="CCC">..</section
<section type="CCC" prevType="CCC">..</section
</section>
<section type="AAA" prevtype="AAA">
the bolded needs to be CCC
so I think you need some logic for the last element in document order that opened before the current one.
That is a trickier beast
Can you confirm that is what you need?