I am new to XSLT and am trying to solve the following transformation.

I have an XML that looks like this...

<Groups>
 <Group>
  <GroupSelector>52</GroupSelector> 
  <GroupDescription>Group 52</GroupDescription> 
    <GroupValue>ABCD</GroupValue> 
 </Group>
 <Group>
  <GroupSelector>27</GroupSelector> 
  <GroupDescription>Group 27</GroupDescription> 
  <GroupValue>PQRS</GroupValue> 
 </Group>
 <Group>
  <GroupSelector>20</GroupSelector> 
  <GroupDescription>Group 20</GroupDescription> 
  <GroupValue>XYZA</GroupValue> 
 </Group>
 <Group>
  <GroupSelector>15</GroupSelector> 
  <GroupDescription>Group 15</GroupDescription> 
  <GroupValue>MNOP</GroupValue> 
 </Group>
</Groups>

There may be 0 to n number of 'Group's

I am trying to apply an XSLT to find a 'Group' where 'GroupSelector' value is 20 and create output like this;

<GroupSelection ElementName="FoundGroup" Missing="false">20</GroupSelection>
<GroupSelection ElementName="GroupDes" Missing="false">Group 20</GroupSelection>
<GroupSelection ElementName="GroupVal" Missing="false">XYZA</GroupSelection>

If none of the n 'Group's have a 'GroupSelector' with value 20, the output should be as such;

<GroupSelection ElementName="FoundGroup" Missing="true"/>
<GroupSelection ElementName="GroupDes" Missing="true"/>
<GroupSelection ElementName="GroupVal" Missing="true"/>

Please help. Thanks in advance.

Lola

Try the below

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="Groups">
	<xsl:choose>
	<xsl:when test="Group/GroupSelector[. = '20']">
	<GroupSelection ElementName="FoundGroup" Missing="false">20</GroupSelection>
<GroupSelection ElementName="GroupDes" Missing="false">Group 20</GroupSelection>
<GroupSelection ElementName="GroupVal" Missing="false">XYZA</GroupSelection>
    </xsl:when>
	<xsl:when test="Group/GroupSelector[ . !='20']">
	<GroupSelection ElementName="FoundGroup" Missing="true"/>
<GroupSelection ElementName="GroupDes" Missing="true"/>
<GroupSelection ElementName="GroupVal" Missing="true"/>
	</xsl:when>
	</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.