[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: Re: [xsl] Java XSLT transformers and document('') problem
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Mon, 8 Sep 2008 10:40:32 +0100

> 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[@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[@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[@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="*[@mode = 'string']">

and

<xsl:template match="*[@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/


Current Thread
Keywords
xml