I have below sample XML:
<?xml version="1.0"encoding="UTF-8"?>
<Root>
<Record>
<JobProfile>
<Status>
<Code>Pending</Code>
</Status>
<ID>
<Code>007</Code>
</ID>
</JobProfile>
<JobProfile>
<Status>
<Code>Active</Code>
</Status>
<ID>
<Code>003</Code>
</ID>
</JobProfile>
<JobProfile>
<Status>
<Code>Terminated</Code>
</Status>
<ID>
<Code>004</Code>
</ID>
</JobProfile>
</Record>
</Root>
I need to get output like below:
<Root>
<Record>
<ID>003</ID>
</Record>
</Root>
The Logic is – Read the input XML, find the node where JobProfile/Status/Code = “Active”. Once found, read the JobProfile/ID/Code at the sibling’s level and write it in the output.
Tried the below XSLT but it’s not working
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Root/Record">
<ID>
<xsl:for-each select="Record/JobProfile">
<xsl:if test="Status/Code='Active'">
<xsl:value-of select="ID/Code"/>
</xsl:if>
</xsl:for-each>
</ID>
</xsl:template>
</xsl:stylesheet>