Filter specific child nodes along with the parent
I need to retrieve the parent nodes along with only those child nodes
matching specific attribute values. The input XML is
<ErrorList>
<Table name="Table1" type="A">
<Error category="Minor">
<errorCode>100</errorCode>
<errorMessage>msg-100</errorMessage>
</Error>
<Error category="Major">
<errorCode>101</errorCode>
<errorMessage>msg-101</errorMessage>
</Error>
</Table>
<Table name="Table2" type="B">
<Error category="Fatal">
<errorCode>102</errorCode>
<errorMessage>msg-102</errorMessage>
</Error>
<Error category="Major">
<errorCode>105</errorCode>
<errorMessage>msg-101</errorMessage>
</Error>
</Table>
</ErrorList>
The XSL code below retrieves the Error nodes having category attribute
Fatal or Minor:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:template match="Error[@category = 'Fatal'] | Error[@category =
'Minor']">
<xsl:copy-of select="self::node()"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
The output I get:
<Error category="Minor">
<errorCode>100</errorCode>
<errorMessage>msg-100</errorMessage>
</Error><Error category="Fatal">
<errorCode>102</errorCode>
<errorMessage>msg-102</errorMessage>
</Error>
The expected output is below (need to retrieve the parent table node for
the select child nodes):
<Table name="Table1" type="A">
<Error category="Minor">
<errorCode>100</errorCode>
<errorMessage>msg-100</errorMessage>
</Error>
</Table>
<Table name="Table2" type="B">
<Error category="Fatal">
<errorCode>102</errorCode>
<errorMessage>msg-102</errorMessage>
</Error>
</Table>
Can you please help me with the xsl to get the desired output.
No comments:
Post a Comment