[xsl] Getting variable yet most immediate parentNode

Subject: [xsl] Getting variable yet most immediate parentNode
From: Steve <subsume@xxxxxxxxx>
Date: Thu, 27 Sep 2007 14:55:15 -0400
XML=

The general gist of the below application is to output a survey and
all its questions, and give the user the appropriate options
(answers).

Problem: given the variable relationship between the <answer> nodes
being matched and their parent <section>, how to reliably amtch the
correct section @name?

<root>
   <section key="A" name="Survey 1">
      <answers>
         <options />
      </answers>
      <questions>
          <answers />
          <question>
                <answer>
                    <option>True</option>
                    <option>False</option>
                </answer>
           </question>
      </questions>
   </section>
</root>


XSL

	<xsl:template match="section">
		<fieldset class="oneCol" id="{@key}">
			<legend><span><xsl:value-of select="@name" /></span></legend>
			<xsl:apply-templates select="questions" />
		</fieldset>
	</xsl:template>
	<xsl:template match="questions">
		<xsl:apply-templates mode="q" select="option" />
		<xsl:if test="following-sibling::questions">
			<hr />
		</xsl:if>
	</xsl:template>
	<xsl:template match="option" mode="q">
		<div>
			<span class="two">
				<label>
					<xsl:choose>
						<xsl:when test="@name">
							<xsl:value-of select="@name"/>
						</xsl:when>
						<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
					</xsl:choose>
				</label>
			</span>
			<xsl:choose>
				<xsl:when test="answers">
					<xsl:apply-templates select="answers">
						<xsl:with-param name="qKey" select="@key" />
					</xsl:apply-templates>
				</xsl:when>
				<xsl:when test="../answers">
					<xsl:apply-templates select="../answers">
						<xsl:with-param name="qKey" select="@key" />
					</xsl:apply-templates>
				</xsl:when>
				<xsl:when test="../../answers">
					<xsl:apply-templates select="../../answers" >
						<xsl:with-param name="qKey" select="@key" />
					</xsl:apply-templates>
				</xsl:when>
				<xsl:when test="../../../answers">
					<xsl:apply-templates select="../../../answers" >
						<xsl:with-param name="qKey" select="@key" />
					</xsl:apply-templates>
				</xsl:when>
			</xsl:choose>
		</div>
	</xsl:template>
	<xsl:template match="answers">
		<xsl:param name="qKey" />
		<xsl:if test="option">
			<select>
				<option>Select one</option>
				<xsl:for-each select="option">
					<option>

                                            <!-- [[ Need help below]] -->

						<xsl:if test="$R[section=current()/*/section/@key and
questionKey=$qKey and (value=current()/@name or value=current()/.)]">
							<xsl:attribute name="selected">selected</xsl:attribute>
						</xsl:if>
						<xsl:choose>
							<xsl:when test="@name">
								<xsl:value-of select="@name"/>
							</xsl:when>
							<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
						</xsl:choose>
					</option>
				</xsl:for-each>
			</select>
			<span class="print">
				<xsl:value-of select="$R[section=current()/*/section/@key and
question=$qKey and value=current()/@name or
value=current()/.]/value"/>

			</span>
		</xsl:if>
	</xsl:template>

Current Thread