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 >
<label >Gender</label>
<div >
<div >
<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>
CodePudding user response:
The way your line
<xsl:if test="Selected = true">
works is:
- The test is an xpath expression
Selectedis 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 nodelist returned by 'true'
Not what you want. You could do something like
<xsl:if test="Selected = true()">
<xsl:if test="Selected">
- it will test if the value is a boolean 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
CustomerGenderwithoutSelectedit will always be true
So you probably want to change that into
<xsl:if test="Selected = 'true'">
which will perform a string comparison. An alternative could be
<xsl:if test="Selected[1] = 'true'">
which will convert the first found element to string (resulting in the text content), then perform the comparison, or
<xsl:if test="Selected[1] = true()">
<xsl:if test="Selected[1]">
which would convert the first element into a boolean (and now I am lost myself...)
