Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling another element by attribute value in same context?

Status
Not open for further replies.

MaKSiNG

Technical User
Dec 4, 2003
19
I have the following XML which define the options available to a radio button and which option is checked as default:

Code:
<item name="options">
   <value>
      <item name="description">
         <value>Yes</value>
      </item>
      <item name="default"/>
   </value>
   <value>
      <item name="description">
         <value>No</value>
      </item>
      <item name="default">
         <value>yes</value>
      </item>
   </value>
</item>

I am using the following piece of XSL to transform this into HTML:

Code:
<xsl:for-each select="value/item/value/item[@name='description']">
<xsl:variable name="pos2" select="position () - 1"/>
<tr valign="top">
<td class="cn-cont-nopad" height="10" width="10">
   <xsl:element name="input">
   <xsl:attribute name="type">radio
   </xsl:attribute>
   <xsl:attribute name="name">
      <xsl:value-of select="$rdBtnName" />
   </xsl:attribute>
   <xsl:attribute name="id">radiobutton0_
      <xsl:value-of select="$pos"/>
   </xsl:attribute>
[COLOR=blue]   <xsl:attribute name="value">radiobutton0_
      <xsl:value-of select="$pos"/>_
      <xsl:value-of select="$pos2"/>
   </xsl:attribute>[/color]
   <xsl:attribute name="class">cn-form-field
   </xsl:attribute>
[COLOR=red]   <xsl:if test="item[@name='default']/value='yes'">
   <xsl:attribute name="checked">checked</xsl:attribute>
   </xsl:if>[/color]
   </xsl:element>
</td>
<td class="cn-form-radio-text" height="10" width="190">
<xsl:value-of select="value" />
</td>
</tr>
</xsl:for-each>

My problem is two-fold:

1. I am building the <input> tag bit by bit using <xsl:element> and <xsl:attribute> and populating the attributes with variables, text and content from the XML. If you look at the value attribute in blue, I am building it using 2 variables and text. When I look at the rendered HTML I get lots of spaces between the various components I am trying to put together in a single string. This stops the HTML being read as a single line and I am not getting a radio button displayed. I have taken the resulting HTML and deleted all the space from these attributes and it works fine. How do I stop all these extra spaces being added?

2. I am having difficulty locating the node I need in the highlighted red <xsl:if> statement. My <xsl:for-each> statement puts me on a certain level of nodes; the item node with attribute description, but how do I now specify the item node with attribute default, which is also on the same level?


Thx,
MaKS
 
First of all, I think you might consider simplifying your xml. For example:
Code:
<options>
  <item default="False">Yes</item>
  <item default="True">No</item>
</options>

Then for your questions:
1: use the concat()-function:
<xsl:attribute name="value">
<xsl:value-of select="concat(radiobutton0_,$pos,'_',$pos2)" />
</xsl:attribute>

2: there is an x-path-expression for parent-node (../), so you can ask if the parent-node has a childnode...etc:
<xsl:if test="../item[@name='default']/value='yes'">
In the xml I suggested above, you don't have to stunt like that...

Enjoy,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top