xsl-list
[Top] [All Lists]

[xsl] Getting variable yet most immediate parentNode

2007-09-27 11:55:45
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="{(_at_)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>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--