Re: [xslt] needed:set xslt variable in program



Hi, 

> -----Original Message-----
> From: xslt-bounces gnome org [mailto:xslt-bounces gnome org] 
> On Behalf Of Tom Stanley
> 
> hello, guys
> we plan to use libxml and libxslt  in our projects.
> what we need are like snippet belows:
>             <ActivityInput>
>                 <body>
>                     <pfx:field>
>                         <pfx:name>
>                             <xsl:value-of
> select="&quot;Data&quot;"/>
>                         </pfx:name>
>                         <pfx:id>
>                             <xsl:value-of select="0"/>
>                         </pfx:id>
>                         <pfx:simple>
>                             <xsl:value-of
> select="string($var/Report/Data[1])"/>
>                         </pfx:simple>
>                     </pfx:field>
> 	 </body>
>             </ActivityInput>
> We want to use something like  xslt template to
> produce some xml tree as process's input.
> We can change this xml snippet to a xslt template and
> use applystylesheet to produce expected output xml
> tree.
> However, we need to set a variable (named var ) in
> program before apply the translation. 
> Does libxslt have any method to do this ,for example
> like xmlXPathRegisterVariable in libxml?
> 
> or any other thoughts?

The common solution for this would be to use the document() function:

<xsl:template match="/">  
  <foo>
    <xsl:value-of select="document('my-report.xml')/Report/Data[1]"/>
  </foo>
</xsl:template>

If you need a more sophisticated mechanism, then you could implement
an extension element/function. Have a look at how e.g. the
EXSLT function node-set() is implemented in Libxslt/Libexslt:
The XPath engine calls exsltNodeSetFunction() (in "libexslt/common.c"),
then xsltFunctionNodeSet() (in "libxslt/extra.c") is called, which
changes a result tree fragment into a node set.

In such an extension function, you could just hand-over a copy of an
internal node tree managed by your application.
<foo>
  <xsl:value-of select="mf:my-func('my-var-name')/Report/Data[1]"/>
</foo>

Regards,

Kasimier






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]