Hi Corinna,

This is a case where you might use an extension function, but you might
want to consider other approaches.  You say you're going to have to "parse"
this image file.  Is it an xml file?  If it is, you could pre-parse it and
pass it in as a param.  Or, you could have your extension function parse
the file once, then cache the result and return the bits that were asked
for.

Repeatedly constructing fragments to return to the caller is very
expensive, and is only necessary when the information is not already
available as XML, or its value depends on another variable.  It's usually
better to consider a solution where the data can be returned as strings or
numbers, depending on its proper type.  If information is returned as a
fragment, it must be returned as string values, which means the end user
will need to search the fragment for the appropriate information, then
perhaps convert it to a number.  That sort of thing can be very expensive.
For example, your "imageWidth" function would probably want to return a
number, so you can construct a variable that contains a number :

   <xsl:variable name="width" select="external:imageWidth(@file)"/>

rather than a variable that contains a result tree fragment like in your
example:

   <xsl:variable name="width">
       <xsl:value-of select="external:imageWidth(@file)"/>
   </xsl:variable>

Is there any reason why your extension function can't cache enough
information to make repeated lookups more efficient?  If you could
elaborate more on the format of your data, it would help.

Dave



|---------+--------------------------->
|         |           [EMAIL PROTECTED]|
|         |           m (Corinna      |
|         |           Kinchin)        |
|         |                           |
|         |           04/11/2002 11:37|
|         |           AM              |
|         |           Please respond  |
|         |           to xalan-dev    |
|         |                           |
|---------+--------------------------->
  
>---------------------------------------------------------------------------------------------------------------------------|
  |                                                                                    
                                       |
  |        To:      [EMAIL PROTECTED]                                           
                                       |
  |        cc:      (bcc: David N Bertoni/Cambridge/IBM)                               
                                       |
  |        Subject: Re: Help! Extension functions ...                                  
                                       |
  
>---------------------------------------------------------------------------------------------------------------------------|



Dave,

Thanks for your feedback, as always.  It's really
appreciated.

To give a bit more background on why I believe an extension
function is necessary: we have a home grown `C' library of
functions which will pull out image file metadata, such as image
width, height, aspect ratio, no of pics and dpi. What we're trying
to do is to give our users the ability to get at this
image data via a Xalan-C++ XSLT extension function.

As a test, I've adapted the ExternalFunction example to
create an imageWidth extension function which returns
the width of a given image file, so something like:

             <xsl:variable name="width">
                         <xsl:value-of select="external:imageWidth
(@file)"/>
             </xsl:variable>

will assign the image width to the variable named `width'.

In principle I could define a whole set of extensions, one
for each item of image meta data ... but the overhead of
repeatedly parsing the (possibly huge) image file makes
this unworkable.

So - I read somewhere that an extension function could return
a nodeset, and thought that this could be a neat solution:
returning an <ImageMetaData ..> element, for example, with the
width and height given as attribute values.

But in the meantime perhaps the result tree fragment could
be the way to go?

Thanks for your patience, and thank you for your great help.
It's been wonderful.

Corinna ...

 Corinna Kinchin            email: [EMAIL PROTECTED]
 Datazone Ltd.              Tel:   +44-(0)20-7221 8026
 133 Notting Hill Gate      Fax:   +44-(0)20-7727 6045
 London W11 3LB, England.   URL:   http://www.miramo.com

 Datazone - makers of `Miramo', the automated publishing tool

> Hi Corinna,
>
> This is a bit tricky in Xalan-C++ as we don't really have a public
> interface for creating fragments.  One thing you can do is to look at the
> code to see how we create things internally, then copy that code into
your
> function.  Other ways include generating the element and attributes using
> xsl:element and xsl:attribute, or creating a result tree fragment and use
> the nodeset extension function to convert it into a nodeset.  Those are
> easier than writing an extension function, provided you have a way to get
> the data from the Width and Height attributes.
>
> Extension functions can be dangerous if they have any side-effects.  They
> also require that you install the function into the run-time.  A result
> tree fragment requires no extra support, aside from the already-supplied
> nodeset extension function.  Also, when moving to XSLT 2.0, there will no
> longer be any need for the nodeset function, so your stylesheet can be
> quickly modified to be portable, rather than proprietary
>
> If you can describe more about the source for the data you'll be using to
> create the element and attributes, perhaps someone can illustrate how you
> can accomplish what you want to do without resorting to an extension
> function.
>
> Dave
>
>
> Hi all,
>
> I'm trying to write an extension function which
> creates a new `Dim' element with `Width' and `Height'
> attributes set to numeric values, and which returns the
> `Dim' element as a node-set. More generally, I want
> to return a set of values from an extension function,
> rather than just a string or number (which is beautifully
> illustrated in the ExternalFunction example).
>
> Being new to this area, I can't for the life of me
> figure out how to do this yet ... I can create
> a node set but how to add an element (node?) to it??
> (haven't quite got a grip on the terminology yet).
>
> If anyone has any pointers (eg code snippets which might
> give me some ideas, they don't have to be complete or perfect),
> I'd really appreciate your feedback.
>
> Thanks!
>
> Yours flummoxedley,
>
>
> Corinna ...
>
>  Corinna Kinchin            email: [EMAIL PROTECTED]
>  Datazone Ltd.              Tel:   +44-(0)20-7221 8026
>  133 Notting Hill Gate      Fax:   +44-(0)20-7727 6045
>  London W11 3LB, England.   URL:   http://www.miramo.com
>
>  Datazone - makers of `Miramo', the automated publishing tool
>





Reply via email to