Here is a complete and probably one of the shortest possible XSLT solutions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" /> <xsl:template match="/*"> <cars> <xsl:copy-of select="car[data[@attrib='Model' and text='855']]"/> </cars> </xsl:template> </xsl:stylesheet>
However, the following conversion using a known rule easier to write and provides maximum flexibility, extensibility and maintainability:
<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="car[not(data[@attrib='Model' and text='855'])]"/> </xsl:stylesheet>
When one of these two transformations is applied to the provided XML document:
<cars> <car> <data attrib="Make"> <text>Volvo</text> </data> <data attrib="Model"> <text>855</text> </data> </car> <car> <data attrib="Make"> <text>Volvo</text> </data> <data attrib="Model"> <text>745</text> </data> </car> <car> <data attrib="Make"> <text>Volvo</text> </data> <data attrib="Model"> <text>V70R</text> </data> </car> </cars>
the desired, correct result is output:
<cars> <car> <data attrib="Make"> <text>Volvo</text> </data> <data attrib="Model"> <text>855</text> </data> </car> </cars>
Explanation
The first conversion generates the top element of cars
, then simply selects the desired element car
and copies it as the body of cars
.
The second transformation is based on one of the most fundamental and powerful XSLT design patterns β the use and redefinition of an identification rule.
An identity template copies each associated node (for which it is selected for processing) "as is".
There is one template that overrides the identification rule. This pattern matches any car
for which it is not true that data[@attrib='Model' and text='855']
. The body of the template is empty, and this does not mean that the matching car
element is copied to the output - in other words, we can say that the element corresponding to the car
element is "deleted".
source share