Jump to content

Passing Parameters To Sort


ameliabob
 Share

Recommended Posts

I am trying to use the same XSL stylesheet to transform the same file but have it sorted on different fields. In the transform process I have the following code

		$process->importStyleSheet($newxsl);	$process->setParameter(NULL, 'sortKey', $sortParam);	return ($process->transformToXML($dom));	

and in the xsl sheet I am trying to use the sortKey as

    <xsl:for-each select="/workingFile/user">    <xsl:sort select='$sortKey' />

I have tried $sortKey with and without the single quotes.What syntax am I missing?

Link to comment
Share on other sites

The select expression in xsl:sort must match an element to sort by. However, the "sortKey" value is not used as an element, but as a string. If you want, you can instead use:

<xsl:sort select="*[local-name() = $sortKey]" />

to sort by the element that's called the same way as the sort key.Remember, parameters set by PHP are only passed as strings to XSLT. XSLT must manually convert them to whatever other data type is needed.

Link to comment
Share on other sites

I am trying to use the same XSL stylesheet to transform the same file but have it sorted on different fields. In the transform process I have the following code
		$process->importStyleSheet($newxsl);	$process->setParameter(NULL, 'sortKey', $sortParam);	return ($process->transformToXML($dom));	

and in the xsl sheet I am trying to use the sortKey as

    <xsl:for-each select="/workingFile/user">    <xsl:sort select='$sortKey' />

I have tried $sortKey with and without the single quotes.What syntax am I missing?

Link to comment
Share on other sites

boen-robot your idea worked so well I thought I would extend it but to no availThe code that you suggested and worked was

<xsl:sort select="*[local-name() = $sortKey]" />

and I extended it as follows

<xsl:sort select="*[local-name() = $sortKey]" order ="*[local-name() = $sortDir]" />

I get the message xsltSortComp: invalid value *[local-name() = $sortDir] for order.I am using the same setParameter function for both. and and passing the words "ascending" and "descending" as the parameter.Is there a clever way to see the parameters in the XSLTProcessor?

Link to comment
Share on other sites

boen-robot your idea worked so well I thought I would extend it but to no availThe code that you suggested and worked was
<xsl:sort select="*[local-name() = $sortKey]" />

and I extended it as follows

<xsl:sort select="*[local-name() = $sortKey]" order ="*[local-name() = $sortDir]" />

I get the message xsltSortComp: invalid value *[local-name() = $sortDir] for order.I am using the same setParameter function for both. and and passing the words "ascending" and "descending" as the parameter.Is there a clever way to see the parameters in the XSLTProcessor?

The value for the order attribute is the value "ascending" or "descending", but not the XPath string "ascending" or "descending". In other words, this is hardcoded into XSLT the same way as... say... the "data-type" attribute. You can only use the method I suggested in places where an XPath expression is expected.The only workaround I can think of is to separate your for-each in a template, then apply this template with one of two sortings based on the value of the parameter. E.g.:
...<!-- The order of templates is not important i.e. this can occur before or after the match="/" template.--><xsl:template match="user">	<!-- Extend this match to the full "/workingFile/user" expression if you want to match it only there. If you have "user" elements at other places that need the same treatment, leave it like that. For this particular example, it won't make a difference, but if you have just <xsl:apply-templates/>, it will. -->	...</xsl:template><xsl:template match="/">	...	<xsl:choose>		<xsl:when test="$sortDir = 'ascending'">			<xsl:apply-templates select="/workingFile/user">				<xsl:sort select="*[local-name() = $sortKey]" order="ascending" />			</xsl:apply-templates>		</xsl:when>		<xsl:otherwise>			<!--You can instead use an additional "when", but in that case, nothing will happen with a third value. In this case, any value other than "ascending" triggers "descending".-->			<xsl:apply-templates select="/workingFile/user">				<xsl:sort select="*[local-name() = $sortKey]" order="descending" />			</xsl:apply-templates>		</xsl:otherwise>	</xsl:choose></xsl:template>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...