[xsl] apply-templates issue

Subject: [xsl] apply-templates issue
From: footh <footh@xxxxxxxxx>
Date: Tue, 6 Sep 2005 12:52:42 -0700 (PDT)
Hello all,

I am trying to convert xml to an html form.  I can
best show what my problem is by example.  Take this
xml:

<xml-to-input>
  <MyFields>
    <Field1 type="string" >
     hello
    </Field1>
    <Field2 type="string" />
    <Field3 type="string" select="1">
      <option value="this">this</option>
      <option value="is">is</option>
      <option value="cool">cool</option>
    </Field3>
    <Field4 type="string" />
</MyFields>						</xml-to-input>

I would like it to create html like this:

My Fields
Field1 [input box]
Field2 [input box]
Field3 [select box with specified options]
Field4 [input box]

I am very close to getting this to work, but have one
problem.  Here are the relevant xsl templates I'm
using:

<xsl:template match="xml-to-input">
  <xsl:apply-templates select="*" mode="xml-to-input"
/>
</xsl:template>

<xsl:template match="*" mode="xml-to-input">
  <tr>
    <td>
      <xsl:value-of select="local-name()"/>
    </td>
    <xsl:choose>
      <xsl:when test="@type">
        <td>
          <xsl:choose>
            <xsl:when test="@select = 1">
              <select>
                <xsl:attribute
name="name"><xsl:value-of
select="local-name()"/></xsl:attribute>
                <xsl:apply-templates select="option"
mode="xml-to-input" />
              </select>
            </xsl:when>
            <xsl:otherwise>
              <input type="text" size="35">
                <xsl:attribute
name="name"><xsl:value-of
select="local-name()"/></xsl:attribute>
                <xsl:attribute
name="value"><xsl:value-of
select="normalize-space(.)"/></xsl:attribute>
              </input>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:when>
      <xsl:otherwise>
        <td>&#160;</td>
      </xsl:otherwise>
    </xsl:choose>
  </tr>
  <xsl:apply-templates select="*"
mode="xml-to-input"/>
</xsl:template>

<xsl:template match="option" mode="xml-to-input">
  <option>
    <xsl:attribute name="value"><xsl:value-of
select="normalize-space(.)"/></xsl:attribute>
    <xsl:value-of select="normalize-space(.)"/>
  </option>
</xsl:template>

Sorry if the formatting of this XML is bad.  The
problem that occurs is when the select box is hit, I
call an apply-templates to deal with the select
"options".  This works fine, except that at the end of
the middle template, I use apply-templates again. 
This has the effect of calling the "option" template
for a second time.  I would've thought that after
calling apply-templates for the "option" tags the
first time, the "pointer" in XPath would move beyond
them.  Anyway, I guess I was wrong - I'm sure this is
standard behavior.  I am new to XSL and am looking for
some insight to make this work.  Thanks a bunch.

-JF



	
		
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/

Current Thread