damik
damik

Reputation: 33

Pass parameter from xquery to xslt

I would like to transform xml using xslt but important variable comes from request. I have such xquery:

let $transform := doc("projekt.xsl")
let $serialization-options := 'method=xml media-type=text/xml omit-xml-declaration=yes indent=no'
let $params := 
<parameters>
    <param name="output.omit-xml-declaration" value="yes"/>
    <param name="output.indent" value="yes"/>
    <param name="output.media-type" value="text/html"/>
    <param name="output.method" value="xhtml"/>
    <param name="param.name" value="topicid" />
    <param name="param.select" value="{$topid}"/>
</parameters>

return 
    transform:transform($doc, $transform, $params, $serialization-options)

file project.xsl is here:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="topicid"/>

<xsl:template match="/">
    <xsl:value-of select="$topicid"/>
    <xsl:apply-templates select="discussflow/message[@topic=$topicid]"/>
</xsl:template>

<xsl:template name="msg" match="//message">
    ..........
</xsl:template>

I would like tu add attribute 'select' to:

<xsl:param name="topicid"/> 

with $topid value specifed in xquery.

I have seen something like that i java here: http://www.techrepublic.com/article/pass-parameters-to-xsl-templates-programmatically/1044596 but in xquery it do not want work.

I use exist db 1.4.1

Edit:

transform:transform is from http://exist-db.org/xquery/transform namespace

Official documentation is here: https://en.wikibooks.org/wiki/XQuery/XQuery_and_XSLT

Upvotes: 0

Views: 1018

Answers (2)

Kirin
Kirin

Reputation: 11

in your xslt doc you need to use:

<xsl:param name="param.select" select="default value" />
<xsl:param name="output.omit-xml-declaration" select="default value""/>
<xsl:param name="output.indent" select="default value"/>
<xsl:param name="output.media-type" select="default value"/>
<xsl:param name="output.method" select="default value"/>
<xsl:param name="param.name" select="default value" />
<xsl:param name="param.select" select="default value"/>

that is, the name of the parameter has to be equal to those defined in your xquery. You may use the select to enter a default value, if there is no such parameter (or you run the xslt without an request, e.g. for testing purposes...)

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163262

I'm not familiar with this API but I don't know where you got this idea from:

<param name="param.name" value="topicid" />
<param name="param.select" value="{$topid}"/>

My reading of the documentation is that if the stylesheet has a parameter named topicid, then I would expect the query to pass something like

<param name="topicid" value="{$topid}"/>

Upvotes: 0

Related Questions