xsl-list
[Top] [All Lists]

Re: [xsl] template matching problem # 18,012

2007-08-23 15:32:34
Forgive me for being vague. Oftentimes I'm not transforming the XML
but rather grabbing it via the document() function (the XML I am
transforming is typically a record of client data and my templates
match those).

I have an XML file 'Definitions.xml' which I bring in via document()
and looks something like:

<root>
   <programs>
     <option>A</option>
     <option>B</option>
     <option>C</option>
   </programs>
  <people>
     <person>Dimitre</person>
     <person>Michael</person>
  </people>
  <department name="Studio">
    <subDepartment>Design</subDepartment>
    <subDepartment>Programming</subDepartment>
    <subDepartment ignore='true'>Janitorial Fun</subDepartment>
   </department>
</root>

My template with mode="select" gives me the precise output I desire,
however, a technicality I don't like here is that in order to get a
dropdown box it seems to me that I must match the parent node in order
to get a list of options of the children. Rather, I'd like to pass my
template the children nodes (because its possible to ignore my @ignore
sample element above). But when I do this I get a dropdown box for
each child.

Sample usage:

<xsl:apply-templates mode="select"
match="document('Definitions.xml')/root/department[not(subDepartment/@ignore)]"

   <xsl:with-param name="id" select="'subDepartments'" />
</xsl:apply-templates>

If I understand the above usage below, the subDepartment with
ignore='true' won't be excluded. Again, my template is below. The
match node is parent (programs, people, department), when I'd rather
have it be the child.

        <xsl:template mode="select" match="node()">
                <xsl:param name="selected" />
                <xsl:param name="id" />
                <xsl:param name="other" />
                <xsl:param name="required" />
                <xsl:param name="otherText">Other</xsl:param>
                <xsl:choose>
                        <xsl:when test="$id=''">
                                ERROR: No id given
                        </xsl:when>
                        <xsl:otherwise>
                                <select id="{$id}" name="{$id}">
                                        <xsl:if test="$required='required'">
                                                <xsl:attribute 
name="required">required</xsl:attribute>
                                        </xsl:if>
                                        <xsl:attribute name="onChange">
                                                if(this.value=='i_Other'){
                                                        new 
Insertion.Before(this,"<input type='text'
id='<xsl:value-of select="$id" />' name='<xsl:value-of select="$id"
/>' />");Element.remove(this);
                                                }
                                        </xsl:attribute>
                                        <option>Select one</option>
                                        <xsl:for-each select="../node()">
                                                <option>
                                                        <xsl:if 
test=".=$selected">
                                                                
<xsl:attribute name="selected">selected</xsl:attribute>
                                                        </xsl:if>
                                                        <xsl:attribute 
name="value">
                                                                <xsl:choose>
                                                                        
<xsl:when test="@key"><xsl:value-of select="@key" /></xsl:when>
                                                                        
<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
                                                                </xsl:choose>
                                                        </xsl:attribute>
                                                        <xsl:value-of 
select="." />
                                                </option>
                                        </xsl:for-each>
                                        <xsl:if test="$other!=''">
                                                <option 
value="i_Other"><xsl:value-of select="$otherText" /></option>
                                        </xsl:if>
                                </select>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>


On 8/23/07, cknell(_at_)onebox(_dot_)com <cknell(_at_)onebox(_dot_)com> wrote:
Your template seems to bear little resemblance to your stated desired output. 
You're leaving out a lot of explanation, aren't you?

If what you want is an option list control based on the example XML, then 
this will do it. Please explain what is missing.
========================

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:strip-space elements="*" />
  <xsl:output method="xml" indent="yes" encoding="UTF-8" />

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

        <xsl:template match="activity">
           <OPTGROUP>
                      <xsl:apply-templates />
           </OPTGROUP>
        </xsl:template>

        <xsl:template match="option">
          <OPTION><xsl:value-of select="."/></OPTION>
        </xsl:template>

</xsl:stylesheet>
--
Charles Knell
cknell(_at_)onebox(_dot_)com - email



-----Original Message-----
From:     Steve <subsume(_at_)gmail(_dot_)com>
Sent:     Thu, 23 Aug 2007 16:05:07 -0400
To:       xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject:  [xsl] template matching problem # 18,012

Hello there,

I created a template to auto-generate html select dropdown boxes based
on some nodes. I can't get it to work precisely how I want it to.

Ideally, I'd like to:

<xsl:apply-templates mode="select"
select="document('XML.xml')/activity/option" />

and have each option become an <option> in my select dropdown.
However, as it is, instead of getting a dropdown box with 3 options
(A, B, C below) I get 3 dropdownboxes.

on the mode="select" template I tried to match both "*", "node()",
".", but these are off.

Have I explained myself well enough?

Thanks,

-Steve

XML== Activities/XML.xml

<activity>
   <option>A</option>
   <option>B</option>
   <option>C</option>
</activity>
XSL===
       <xsl:template match="/">
                <xsl:apply-templates mode="select"
select="document('../../Activities/XML.xml')/activity/option">
                        <xsl:with-param name="id" select="'program'" />
                </xsl:apply-templates>




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