xsl-list
[Top] [All Lists]

Re: [xsl] Getting variable yet most immediate parentNode

2007-09-28 08:57:10
Here are my templates, once again commented and updated with some of
the advice here.

<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 select="option" />
        <xsl:if test="following-sibling::questions">
                <hr />
        </xsl:if>
</xsl:template>
<xsl:template match="answers[option]">
        <xsl:param name="value" />
        <select name="value">
                <option>Select one</option>
                <xsl:apply-templates select="option" >
                        <xsl:with-param name="value" select="$value" />
                </xsl:apply-templates>
        </select>
        <span class="print">
                <xsl:value-of select="$value"/>
        </span>
</xsl:template>
<xsl:template match="option[name(..) = 'questions']">
        <div>
                <span class="two">
                        <input type="hidden" name="section" 
value="{../../@key}" />
                        <input type="hidden" name="questionKey" 
value="{(_at_)key}" />
                        <label>
                                <!-- Doesn't work. Ignores all @name -->
                                <xsl:value-of select="(@name|.)[1]" />
                        </label>
                </span>
                <xsl:variable name="v" select="$R[section=current()/../../@key 
and
questionKey=current()/@key]/value" />

               <!-- This is only slightly better than the previous
version. Case statements still seem necessary, because i want either
the child, and failing that the nearest ancestor -->

                <xsl:choose>
                        <xsl:when test="answers">
                                <xsl:apply-templates select="answers">
                                        <xsl:with-param name="value" 
select="$v" />
                                </xsl:apply-templates>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:apply-templates 
select="ancestor::*[answers][1]/answers">
                                        <xsl:with-param name="value" 
select="$v" />
                                </xsl:apply-templates>
                        </xsl:otherwise>
                </xsl:choose>
        </div>
</xsl:template>
<xsl:template match="option[name(..) = 'answers']">
        <xsl:param name="value" />
        <option>
                <xsl:if test="$value=(@name|.|@value)">
                        <xsl:attribute name="selected">selected</xsl:attribute>
                </xsl:if>
                <xsl:attribute name="value">
                        <!-- much like value-of select="(@name|.)[1]"
above. Except since that didn't work there's no sense applying it
here, too -->
                        <xsl:choose>
                                <xsl:when test="@value"><xsl:value-of 
select="@value" /></xsl:when>
                                <xsl:otherwise><xsl:value-of select="." 
/></xsl:otherwise>
                        </xsl:choose>
                </xsl:attribute>
                <xsl:value-of select="." />
        </option>
</xsl:template>         

On 9/28/07, Steve <subsume(_at_)gmail(_dot_)com> wrote:
Abel Braaksma <abel(_dot_)online(_at_)xs4all(_dot_)nl> wrote:
which means that <xsl:template match="option" mode="q">... will never be
triggered by this snippet:

    <xsl:template match="questions">
        <xsl:apply-templates mode="q" select="option" />
        <SNIP />
    </xsl:template>

Yep, that's my fault. Questions do have option children. My mistake.

Steve, if you rewrite statements like that as this one above as:

    <xsl:template match="answers[option]">
       ....

Ahh, yes. Wise and simple, thank you. Done.


Wendell, considering the possible intent of Steve to wrap all output
inside one <div>, it is probably better if he does go back to using the
// syntax, but not in the way I proposed, but more like this (assuming

I think we've gone awry. .//answer's doesn't make sense because, as
Wendell pointed out, I want to go up the tree if there is no child
<answers />. I tried his syntax, however I'm not sure how to make it
choose a child answer's node before it goes looking for the closest
ancestral answer node (without using those pesky case statements).


<xsl:template match="option">
    <option><xsl:value-of select="(@name | .)[1]" />
</xsl:template>

And finally, this last shortcut doesn't function. My logic is "If it
has a @name, use it, otherwise just use the node's text." I'm honestly
not sure how to interpret this shortcut because the "|" seems to imply
union, which of course doesn't seem to fit my intentions. At any rate,
this statement never spits out question <option>s with @name.

I'm assuming much of the confusion over what I am attempting is my
fault, and particularly my pasting a slightly incomplete source XML.
Here is an actual example.

<root>
                <section key="D" name="Nutritional status">
                        <questions>
                                <option key="A" name="Does client have 
prescribed diet by physician?" />
                                <option key="C" name="Do you have a good 
appetite?" />
                                <option key="K" name="Daily fluid intake per 
cups?">
                                        <answers>
                                                <option p="1">1</option>
                                                <option p="2">2</option>
                                                <option p="3">3</option>
                                                <option p="4">4</option>
                                                <option p="5">5</option>
                                        </answers>
                                </option>
                                <option key="J" name="Weight">
                                        <answers>
                                                <option 
p="1">Underweight</option>
                                                <option 
p="2">Overweight</option>
                                                <option p="0">Normal</option>
                                        </answers>
                                </option>
                                <option key="L" name="Taking vitamin/mineral 
supplements?" />
                        </questions>
                        <questions>
                                <answers>
                                        <option p="1">5</option>
                                        <option p="2">1</option>
                                        <option p="3">1.5</option>
                                        <option p="4">2</option>
                                        <option p="5">3</option>
                                        <option p="6">4 or more</option>
                                </answers>
                                <option key="D" value="POULTRY">Servings of 
meat, fish, eggs,
dried peas, beans and nuts</option>
                                <option key="E" value="FRUIT">Servings of 
citrus fruit, juices and
tomatoes</option>
                                <option key="F" value="GREENVEG">Servings of 
dark green, leafy
vegetables</option>
                                <option key="G" value="VEG">Servings of other 
fruits, vegetables
and juices</option>
                                <option key="H" value="STARCH">Servings of 
bread, cerea, rice,
pasta</option>
                                <option key="I" value="DAIRY">Servings of 
dairy products</option>
                                <option key="J" value="MISC">Servings of 
Misc. foods</option>
                        </questions>
                </section>
</root>


--~------------------------------------------------------------------
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>
--~--