Sorry but a total xslt noob here
Given XML that looks like:
<Foo>
<Bar />
<Baz />
<Qax />
<FooBar />
</Foo>
Is there an XSLT that will limit the number of child nodes under Foo so that there are only 3?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This transformation uses and overrides the identity rule/template:
When applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
The identity rule/template copies every node “as-is”.
We have just one additional template that overrides the identity rule for any element that is a child of the top element with position greater than 3. This template dos nothing (has an empty body), which effectively prevents any such element from being copied to the output (or as we use to say, “deletes” it).
Do note:
Using and overriding the identity rule is the most fundamental and powerful XSLT design pattern.
Using this design pattern is recommended over a simple
<xsl:copy-of>, because it allows the nodes not only to be copied, but to be processed by any template we provide. Attributes of all elements are also processed.