I am using XSLT 1.0 and I’m looking to divide a xml document of for example related people in to families. The definition of family being one parent and its children.
So this:
<person name =”A”>
<person name =”B”>
<person name =”D” />
<person name =”E” />
</person>
<person name=”C”>
<person name =”F” />
<person name =”G” />
<person name =”H” />
</person>
</person>
Will result in:
<family id =”1”>
<person name =”A”>
<person name =”B”/>
<person name=”C”/>
</person>
</family>
<family id =”2”>
<person name =”B”>
<person name =”D” />
<person name =”E” />
</person>
</family>
<family id =”3”>
<person name=”C”>
<person name =”F”/>
<person name =”G”/>
<person name =”H”/>
</person>
</family>
How can I do this in a simple way?
One way is to firstly look for all person elements that have other person elements as children
Then, for each such person element, you would copy the element, and its immediate children only
Also, to get the family id, you could count all preceding person elements, and ancestors
The “child” template would just copy the person and its attributes, but no children.
Here is the full XSLT
When applied to your sample XML, the following is output