[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

apply-templates issue

Subject: apply-templates issue
From: footh <footh@xxxxxxxxx>
Date: Tue, 6 Sep 2005 12:52:42 -0700 (PDT)
footh
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.