Re: [xsl] How to retrieve Value of global parameter by matching ID.

Subject: Re: [xsl] How to retrieve Value of global parameter by matching ID.
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 06 Sep 2002 12:51:34 -0400
Serhiy,

Your difficulty is not in the way you've designed your <fld> element; that's fine. Rather, it's in how you match up that element's @id to the bit of data you actually want. XSLT gives you no way to select a variable or parameter dynamically by name (unless you adapted, for example, Dimitre Novatchev's techniques in FXSL -- which could be slick, but I'll leave that to him to explain).

There are other ways you could go about it, however, with particular advantages of their own. If, instead of passing in the values as distinct parameters, your servlet wrote a little file like this before invoking the transform:

<parameters>
<param name="client.name">Jhon Smith</param>
<param name="client.address">XX Somestreet, Sometown</param>
<param name="client.postcode">NNN NNN</param>
</parameters>

you could then query into this document to get the values you need using a "lookup table" technique.

<xsl:variable name="parameters" select="document('parameters.xml')/*/param"/>

<xsl:template match="fld">
  <xsl:variable name="parameter" select="$parameters[@name = current()/@id]"/>
  <xsl:value-of select="$parameter"/>
  <xsl:if test="not($parameter)">
    <xsl:text>The field named [" </xsl:text>
    <xsl:value-of select="@id"/>
    <xsl:text>"] is not found!</xsl:text>
  </xsl:if>
</xsl:template>

and you could add parameters with new names at will.

I bet some architectures will let you pass in an entire <parameters> node set as an XSLT parameter (so you wouldn't have to write a file and use the document() function), but I'm no expert in that.

I hope that helps,
Wendell

At 05:26 AM 9/6/2002, you wrote:
Hello,

I recently started to work with XSLT and have this question about global
parameters.

I work with web-based application, which produces PDF documents, using
FO-XML.
The documents include some dynamic fields.
The values of those fields are coming from database and passed as global
parameters from servlet into XML.

Snip of XML file:
========================================
<para>
        This is the value of Name filed: <fld id="client.name" />
        This is the value of Address filed: <fld id="client.address" />
        This is the value of PostCode filed: <fld id="client.postcode" />
</para>
========================================

Here is the associated XSL file:

========================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" encoding="iso-8859-1"/>

  <xsl:param name="client.name"/>
  <xsl:param name="client.address"/>
  <xsl:param name="client.postcode"/>
....
....
 <xsl:param name="producer.name"/>
 <xsl:param name="producer.addr1"/>
....

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

<xsl:template match="fld">
      <xsl:choose>
        <xsl:when test="@id='client.name'">         <xsl:value-of
select="$client.name"/></xsl:when>
        <xsl:when test="@id='client.address'">     <xsl:value-of
select="$client.address"/></xsl:when>
        <xsl:when test="@id='client.postcode'">   <xsl:value-of
select="$client.postcode"/></xsl:when>
....
....
        <xsl:when test="@id='producer.name'">      <xsl:value-of
select="$producer.name"/></xsl:when>
        <xsl:when test="@id='producer.address'">   <xsl:value-of
select="$producer.address'"/></xsl:when>
....
        <xsl:otherwise>
           <xsl:text>The field named [" </xsl:text>
           <xsl:value-of select="@id"/>
           <xsl:text>"] is not found!</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
==========================================

The result of transformation looks something like that:

========================================
        This is the value of Name filed: Jhon Smith
        This is the value of Address filed: XX Somestreet, Sometown
        This is the value of PostCode filed: NNN NNN

========================================
Where Jhon Smith, XX Somestreet, Sometown, NNN NNN - are values of
corresponding fields in database.



At the moment I have to add new [ <xsl:when test="@id='...'"> ...</xsl:when>
] entry for <fld> tag in XSL
each time there is a filed with new ID to be added in the documents.
There are already more than one hundred fields in the documents and the
number is growing.

My question is:
Is there a better way to design <fld> tag?
In other words, is it possible to write some generic code, which will return
value of global parameter by matching fld[@id]
without need to update <fld> tag in XSL every time?


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread