简体   繁体   中英

Using XML attribute value as XSL variable

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM