0

So I am trying to generate xml based on an xml boolean radio option. But as I am new to xsl in general, the variable concept at this depth is a bit out of my reach.

My XML:

<item name="commentOutNode" pathid="commentOutNode">
  <radio>
    <option label="Yes" value="true"/>
    <option label="No" value="false" selected="t"/>
  </radio>
</item>

My Current XSL:

<xsl:variable name="commentOutNode" select="commentOutNode/@value[.]"/>
***
(a ways down)
***
<xsl:when test="$commentOutNode = true">
    # do this stuff
</xsl:when>
<xsl:otherwise>
    # do this other stuff
</xsl:otherwise>

How can I leverage the selected radio option's value for the xsl variable and then test what it is?

1
  • Fixed it.. sorry I have another section with commentOutFarm as well. This code section will be duplicated for farms just as it would be created for nodes. Commented Sep 6, 2013 at 20:31

1 Answer 1

2

To get the value attribute of the selected option of the item with name commentOutNode use the XPath:

//item[@name='commentOutNode']/radio/option[@selected='t']/@value  

To set a variable with such value:

<xsl:variable name="selectedValue" select="//item[@name='commentOutNode']/radio/option[@selected='t']/@value"/>  

To test the variable:

<xsl:choose>
   <xsl:when test="$selectedValue='true'">
      . . . 
   </xsl:when>
   <xsl:otherwise>
       . . . 
   </xsl:otherwise>
</xsl:choose>
Sign up to request clarification or add additional context in comments.

3 Comments

wouldn't you use //item[@name='commentOutNode']/radio/option[@value='true'] to leverage the true/false value?
Or wouldn't this be correct //item[@name='commentOutNode']/radio/option/@value and then <xsl:when test="$selectedValue='true'">
I am not sure what are you after. //item[@name='commentOutNode']/radio/option[@value='true'] returns the option element with attribute value equal true - that is not the selected one though, isn't it? //item[@name='commentOutNode']/radio/option/@value return a node-set with all the values - comparing it with a string just compares the first one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.