xsl-list
[Top] [All Lists]

Re: [xsl] Java XSLT transformers and document('') problem

2008-09-08 05:40:54
Of course, this was very simplified example.
I do this i.e. when generate web forms, based on input xml:

XML:
<document>
<name value="lala" mode="string"/>
<password value="dede" mode="password" />
</document>

XSL:
  <xsl:variable name="doc" select="/"/>

  <xsl:template name="field_type_string"
match="xsl:template[(_at_)name='field_type_string']">
      <xsl:param name="i"/>
      <tr>
          <xsl:call-template name="controlset_caption">
              <xsl:with-param name="i" select="$i"/>
          </xsl:call-template>
          <td class="inp">
              <input type="text" name="{name($i)}" id="{name($i)}"
value="{$i/@value}" class="frm"/>
          </td>
      </tr>
     <!-- example retrieve some other data from xml -->
     <xsl:for-each select="$doc/*"></xsl:for-each>
  </xsl:template>

  <xsl:template name="field_type_password"
match="xsl:template[(_at_)name='field_type_password']">
      <xsl:param name="i"/>
      <tr>
          <xsl:call-template name="controlset_caption">
              <xsl:with-param name="i" select="$i"/>
          </xsl:call-template>
          <td class="inp">
              <input type="password" name="{name($i)}" id="{name($i)}"
value="{$i/@value}" class="frm"/>
          </td>
      </tr>
  </xsl:template>

  <xsl:template name="caller">
      <xsl:param name="n"/>
      <xsl:param name="i"/>
      <xsl:apply-templates 
select="document('')/*/xsl:template[(_at_)name=$n]">
          <xsl:with-param name="i" select="$i"/>
      </xsl:apply-templates>
  </xsl:template>

<xsl:template match="/">
<xsl:for-each select="/document/*">
          <xsl:call-template name="caller">
                    <xsl:with-param name="n" select="concat('field_type_',
@mode)"/>
                    <xsl:with-param name="i" select="."/>
          </xsl:call-template>
  </xsl:for-each>
</xsl:template>


of course, in these templates can be more complex logic, to access to source
XML I use global variable $doc
I do not see how I can replace call to these templates with variables

Nightmare....

Instead of:

<xsl:for-each select="/document/*">
          <xsl:call-template name="caller">
                    <xsl:with-param name="n"
select="concat('field_type_', @mode)"/>
                    <xsl:with-param name="i" select="."/>
          </xsl:call-template>
  </xsl:for-each>

just do:

<xsl:apply-templates select="/document/*"/>

or even just <xsl:apply-templates/>

and then have:

<xsl:template match="*[(_at_)mode = 'string']">

and

<xsl:template match="*[(_at_)mode = 'password']">


As a rule to follow, don't use xsl:for-each or xsl:call-template until
you have mastered xsl:apply-templates....



-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

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