select - XSLT for segmenting XML -
i have xml goes this:
<company> <employee name="john"/> <employee name="sarah"/> <employee name="kim"/> <employee name="karl"/> <employee name="tom"/> <employee name="jim"/> <employee name="sandy"/> </company>
how can use xslt template selecting first n nodes, 3 example, can get:
<company> <employee name="john"/> <employee name="sarah"/> <employee name="kim"/> </company>
in oxygen xml editor, can use following xpath achieve that:
/company/employee[position() < (last() - count(/company/employee)+4)]
but need use xslt in case
help
how can use xslt template selecting first n nodes, 3 example, can get:
<company> <employee name="john"/> <employee name="sarah"/> <employee name="kim"/> </company>
the short answer: knowing little bit xpath , xslt.
complete(but still short) answer:
this transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="employee[position() > 3]"/> </xsl:stylesheet>
when applied on provided xml document:
<company> <employee name="john"/> <employee name="sarah"/> <employee name="kim"/> <employee name="karl"/> <employee name="tom"/> <employee name="jim"/> <employee name="sandy"/> </company>
produces wanted, correct result:
<company> <employee name="john"/> <employee name="sarah"/> <employee name="kim"/> </company>
do note:
the identity rule used copy every node "as-is".
there 1 specific template overriding identity template. matches
employee
element position in node-list greater 3. template has empty body, discarding matched elements.
Comments
Post a Comment