I have this data that is being used with this xslt file that I can’t get the Selected true to set the input radio to be checked. Is this the correct logic for checking test="Selected = true" to set that attribute?
Data
<CustomerGender>
<CustomerGender>
<Value>802</Value>
<Text>Female</Text>
<Selected>false</Selected>
</CustomerGender>
<CustomerGender>
<Value>803</Value>
<Text>Male</Text>
<Selected>false</Selected>
</CustomerGender>
<CustomerGender>
<Value>804</Value>
<Text>Non-binary</Text>
<Selected>true</Selected>
</CustomerGender>
<CustomerGender>
<Value>805</Value>
<Text>Prefer not to answer</Text>
<Selected>false</Selected>
</CustomerGender>
<CustomerGender>
<Value>806</Value>
<Text>Other (please specify)</Text>
<Selected>false</Selected>
</CustomerGender>
</CustomerGender>
XSLT
<div class="col-md-5">
<label class="control-label">Gender</label>
<div class="form-group">
<div class="radio-list">
<label>
<xsl:for-each select ="Customer/CustomerGender/CustomerGender">
<input type="radio" name="selectedGender">
<xsl:attribute name="id" >
<xsl:value-of select="Value"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="Value"/>
</xsl:attribute>
<xsl:if test="Selected = true">
<xsl:attribute name="checked">"checked"</xsl:attribute>
</xsl:if>
<xsl:value-of select="Text"/>
</input>
   <br />
</xsl:for-each>
</label>
</div>
</div>
</div>
>Solution :
The way your line
<xsl:if test="Selected = true">
works is:
- The test is an xpath expression
- Selected is an element name on the child axis
- this part will return a nodelist containing all elements with name Selected
- then it will test if the value is the same as the boolean value true
- for this the nodelist has to be converted to boolean, which is true if the nodelist is not empty
- since in none of your iterations there is a CustomerGender without Selected it will always be true
So you probably want to change that into
<xsl:if test="Selected = 'true'">
which will perform a string comparison.