Link to home
Start Free TrialLog in
Avatar of Letterpart
LetterpartFlag for United Kingdom of Great Britain and Northern Ireland

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:

<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>

Open in new window


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>

Open in new window



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?
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

preceding-sibling will only give you the section on the same level
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?
Avatar of Letterpart

ASKER

Yes, you're correct Geert - it's the trickier one that I need.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's great - working fine - thanks very much Geert.
welcome, fun challenge actually