By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,190 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,190 IT Pros & Developers. It's quick & easy.

Use variable to sort elements

P: n/a
Hi, I want to use a variable to sort elements. That var his passed with
query string (I'm using Perl CGI to generate XML).
Here's a sample of my output:
---------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xml" href="RR.xsl"?>
<!-- $Id: template.xml,v 1.5 2006/12/11 11:13:30 bcochofel Exp $ -->
<RR
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://cochofel.sytes.net
~bcochofel/iweb/files/RR.xsd">
<Request>
<url>/~bcochofel/iweb/cgi-bin/getmysites.cgi</url>
<session>
<user>gu***@cochofel.net</user>
</session>
<query>
<!-- o campo parametro pode ter varias ocurrencias -->
<param name="sort" value="asc" />
<param name="field" value="" />
</query>
</Request>
<Response>
<sites>
<site url="http://www.google.com">
<tag name="pesquisa" weight="30" />
<tag name="procura" weight="30" />
</site>
<site url="http://www.google.com/webhp?complete=1">
<tag name="ajax" weight="25" />
<tag name="pesquisa" weight="30" />
<tag name="procura" weight="30" />
</site>
<site url="https://webmail.fe.up.pt">
<tag name="email" weight="20" />
<tag name="feup" weight="20" />
</site>
</sites>
</Response>
</RR>
---------------------------------------------

I want to use the param sort and is value to sort the element <site>.
In this example:
<param name="sort" value="asc" />
so sort=asc

How can I do this? I'm new to XML, XSL and XPath...

Dec 12 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
bcochofel wrote:
I want to use the param sort and is value to sort the element <site>.
In this example:
<param name="sort" value="asc" />
so sort=asc
I can't give you specific advice since your question isn't well defined.
But here are some general pointers...

First, study the xsl:sort directive, so you understand how to do basic
sorting.

It isn't at all clear from your example what kinds of control you want,
since there's no mention of "asc" anywhere else in your example, so your
next step would be to come up with a better description of that. While
you're at it, you need to say more explicitly what you're trying to use
as your sort key -- the URL? the total weight? something else?

Then the problem becomes one of how to achieve the control you need. Not
everything in XSLT can be easily controlled via variables or parameters.
You may need to explicitly test the parameter's value and use that to
control a conditional. In extreme cases you may find yourself forced to
use extension functions. But until the first two points are addressed, I
really can't advise you on this one.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Dec 12 '06 #2

P: n/a
Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.

On Dec 12, 1:01 pm, Joe Kesselman <keshlam-nos...@comcast.netwrote:
bcochofel wrote:
I want to use the param sort and is value to sort the element <site>.
In this example:
<param name="sort" value="asc" />
so sort=ascI can't give you specific advice since your question isn't well defined.
But here are some general pointers...

First, study the xsl:sort directive, so you understand how to do basic
sorting.

It isn't at all clear from your example what kinds of control you want,
since there's no mention of "asc" anywhere else in your example, so your
next step would be to come up with a better description of that. While
you're at it, you need to say more explicitly what you're trying to use
as your sort key -- the URL? the total weight? something else?

Then the problem becomes one of how to achieve the control you need. Not
everything in XSLT can be easily controlled via variables or parameters.
You may need to explicitly test the parameter's value and use that to
control a conditional. In extreme cases you may find yourself forced to
use extension functions. But until the first two points are addressed, I
really can't advise you on this one.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Dec 12 '06 #3

P: n/a
bcochofel wrote:
Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.
Thanks for the clarification.

Since xsl:sort's attibutes have to be literals, the only way I know to
do this is to use a conditional (probably xsl:choose) to examine the
variable's value and select between two blocks of code, one that sorts
ascending and another that sorts descending.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #4

P: n/a
Thanks very much. Can you give an example of how can I examine the
variable's value? Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...

On Dec 12, 2:29 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:
bcochofel wrote:
Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.Thanks for the clarification.

Since xsl:sort's attibutes have to be literals, the only way I know to
do this is to use a conditional (probably xsl:choose) to examine the
variable's value and select between two blocks of code, one that sorts
ascending and another that sorts descending.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #5

P: n/a
Thanks very much. Can you give an example of how can I examine the
variable's value? Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...
Can I use:
"/RR/Request/query/param[@name='sort']" and
"/RR/Request/query/param[@value]"?
On Dec 12, 2:54 pm, "bcochofel" <bruno.cocho...@gmail.comwrote:
Thanks very much. Can you give an example of how can I examine the
variable's value? Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...

On Dec 12, 2:29 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:
bcochofel wrote:
Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.Thanks for the clarification.
Since xsl:sort's attibutes have to be literals, the only way I know to
do this is to use a conditional (probably xsl:choose) to examine the
variable's value and select between two blocks of code, one that sorts
ascending and another that sorts descending.
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #6

P: n/a
Joseph Kesselman wrote:
bcochofel wrote:
>Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.
Since xsl:sort's attibutes have to be literals,

Some of those attribute values (i.e. order, lang, data-type, case-order)
allow attribute value templates to be used so you can do e.g.

<xsl:param name="sort-order" select="'ascending'"/>

and then somewhere

<xsl:apply-templates select="something">
<xsl:sort select="." order="{$sort-order}"/>
</xsl:apply-templates>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 12 '06 #7

P: n/a
bcochofel wrote:
Thanks very much. Can you give an example of how can I examine the
variable's value?
Sketch of one solution follows. Fill in the elipses with real code, of
course, and wrap this in the appropriate template context.

<xsl:choose>
<xsl:when test="$sort = 'asc'">
<xsl:for-each ...>
<xsl:sort ... order="ascending" />
...
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each ...>
<xsl:sort ... order="descending" />
...
</xsl:for-each>
</xsl:when>
</xsl:choose>
Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...
Can I use:
"/RR/Request/query/param[@name='sort']" and
"/RR/Request/query/param[@value]"?
On Dec 12, 2:54 pm, "bcochofel" <bruno.cocho...@gmail.comwrote:
>>Thanks very much. Can you give an example of how can I examine the
variable's value? Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...

On Dec 12, 2:29 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:

>>>bcochofel wrote:

Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.Thanks for the clarification.
>>>Since xsl:sort's attibutes have to be literals, the only way I know to
do this is to use a conditional (probably xsl:choose) to examine the
variable's value and select between two blocks of code, one that sorts
ascending and another that sorts descending.
>>>--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden


--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #8

P: n/a
Martin Honnen wrote:
Some of those attribute values (i.e. order, lang, data-type, case-order)
allow attribute value templates
Hm. Thought I checked for that in the spec, but I may have misread...?
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #9

P: n/a
I'm trying:

<xsl:template match="/RR/Request/query/param">
<xsl:choose>
<xsl:when test="(@name='sort' and @value='asc')">
<xsl:param name="sort-order" select="'ascending'"/>
</xsl:when>
<xsl:otherwise>
<xsl:param name="sort-order" select="'descending'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

But this doesn't seen to be valid... I'm getting an error with the
xsl:param

On Dec 12, 3:04 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:
bcochofel wrote:
Thanks very much. Can you give an example of how can I examine the
variable's value?Sketch of one solution follows. Fill in the elipses with real code, of
course, and wrap this in the appropriate template context.

<xsl:choose>
<xsl:when test="$sort = 'asc'">
<xsl:for-each ...>
<xsl:sort ... order="ascending" />
...
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each ...>
<xsl:sort ... order="descending" />
...
</xsl:for-each>
</xsl:when>
</xsl:choose>

Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...
Can I use:
"/RR/Request/query/param[@name='sort']" and
"/RR/Request/query/param[@value]"?
On Dec 12, 2:54 pm, "bcochofel" <bruno.cocho...@gmail.comwrote:
>Thanks very much. Can you give an example of how can I examine the
variable's value? Sorry, I'm not really into XPath, I'm still getting
problems with matching elements...
>On Dec 12, 2:29 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:
>>bcochofel wrote:
>>>Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.Thanks for the clarification.
>>Since xsl:sort's attibutes have to be literals, the only way I know to
do this is to use a conditional (probably xsl:choose) to examine the
variable's value and select between two blocks of code, one that sorts
ascending and another that sorts descending.
>>--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #10

P: n/a
Joseph Kesselman wrote:
Martin Honnen wrote:
>Some of those attribute values (i.e. order, lang, data-type,
case-order) allow attribute value templates

Hm. Thought I checked for that in the spec, but I may have misread...?
Third paragraph in <http://www.w3.org/TR/xslt#element-sortsays
"The following optional attributes on xsl:sort control how the list
of sort keys are sorted; the values of all of these attributes are
interpreted as attribute value templates."
and then lists (and explains) the attributes I have listed above.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 12 '06 #11

P: n/a
In my error of validation I'm not using xsl:sort (I'll use it on
another place). I'm just trying to check for the value of sort param.

On Dec 12, 3:36 pm, Martin Honnen <mahotr...@yahoo.dewrote:
Joseph Kesselman wrote:
Martin Honnen wrote:
Some of those attribute values (i.e. order, lang, data-type,
case-order) allow attribute value templates
Hm. Thought I checked for that in the spec, but I may have misread...?Third paragraph in <http://www.w3.org/TR/xslt#element-sortsays
"The following optional attributes on xsl:sort control how the list
of sort keys are sorted; the values of all of these attributes are
interpreted as attribute value templates."
and then lists (and explains) the attributes I have listed above.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 12 '06 #12

P: n/a
bcochofel wrote:
But this doesn't seen to be valid... I'm getting an error with the
xsl:param
xsl:param can't be set at arbitrary locations; it has to be set at the
top of your template.

xsl:variable can be, but is scoped at the place where it's set, so your
code still wouldn't work.

The solution is to invert the problem, using the conditional to decide
what value to assign rather than trying to make the assignment itself
conditional. For example:

<xsl:variable name="sort-order">
<xsl:choose>
<xsl:when test="(@name='sort' and @value='asc')">
<xsl:text>ascending</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>descending</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #13

P: n/a
This block of code sets param name="sort-oder" with the value that
xsl:text outputs?!
Ok, I have to read more on XSLT. Thanks

Joseph Kesselman wrote:
bcochofel wrote:
But this doesn't seen to be valid... I'm getting an error with the
xsl:param

xsl:param can't be set at arbitrary locations; it has to be set at the
top of your template.

xsl:variable can be, but is scoped at the place where it's set, so your
code still wouldn't work.

The solution is to invert the problem, using the conditional to decide
what value to assign rather than trying to make the assignment itself
conditional. For example:

<xsl:variable name="sort-order">
<xsl:choose>
<xsl:when test="(@name='sort' and @value='asc')">
<xsl:text>ascending</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>descending</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #14

P: n/a
bcochofel wrote:
This block of code sets param name="sort-oder" with the value that
xsl:text outputs?!
Well, actually it sets the variable named sort-order, but the same idea
would work for setting the value of a param.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Dec 12 '06 #15

P: n/a
Some of those attribute values (i.e. order, lang, data-type, case-order)
allow attribute value templates to be used so you can do e.g.
I think all attributes with the exception of the "select" one allow AVTs.
Cheers,
Dimitre Novatchev

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:45***********************@newsspool4.arcor-online.net...
Joseph Kesselman wrote:
>bcochofel wrote:
>>Well, I'm using xsl:sort to sort the url attribute of site. What I want
is to control the order (ascending or descending) from the value of the
sort param name.

>Since xsl:sort's attibutes have to be literals,


Some of those attribute values (i.e. order, lang, data-type, case-order)
allow attribute value templates to be used so you can do e.g.

<xsl:param name="sort-order" select="'ascending'"/>

and then somewhere

<xsl:apply-templates select="something">
<xsl:sort select="." order="{$sort-order}"/>
</xsl:apply-templates>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Dec 13 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.