Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: selecting the maximal from all counts(newbie)

Reply
Thread Tools

XSLT: selecting the maximal from all counts(newbie)

 
 
R
Guest
Posts: n/a
 
      01-16-2005
Hello everybody.

I've got a problem with selecting the maximal value of few counts.
I want to count how many nodes are inside the <group> nodes and select
the maximal value.

Given XML:
<root id="123">
<group>
<field>qwq</field>
<field>lala</field>
</group>
<sample>tekst</sample>
<group>
<sample>qwq</sample>
<sample>lala</sample>
<sample>lala2</sample>
</group>
<field>sth else</field>
</root>

If I do sth like this:
<xsl:variable name="Elements" select="count(root/group/*)"/>
the variable's Elements value is 5 - aggregate 'count' of all nodes
inside all the groups.

how should I write my count statement to select ony the maximal value
of nodes in specific group?

thanks in advance
best regards R

 
Reply With Quote
 
 
 
 
Ed Beroset
Guest
Posts: n/a
 
      01-16-2005
R wrote:
> Hello everybody.
>
> I've got a problem with selecting the maximal value of few counts.
> I want to count how many nodes are inside the <group> nodes and select
> the maximal value.
>
> Given XML:
> <root id="123">
> <group>
> <field>qwq</field>
> <field>lala</field>
> </group>
> <sample>tekst</sample>
> <group>
> <sample>qwq</sample>
> <sample>lala</sample>
> <sample>lala2</sample>
> </group>
> <field>sth else</field>
> </root>
>
> If I do sth like this:
> <xsl:variable name="Elements" select="count(root/group/*)"/>
> the variable's Elements value is 5 - aggregate 'count' of all nodes
> inside all the groups.
>
> how should I write my count statement to select ony the maximal value
> of nodes in specific group?


Here's one way to do it:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnssd="http://www.w3.org/2001/XMLSchema"
>

<xslutput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="root/group">
<xsl:sort select="count(./*)" order="descending" />
</xsl:apply-templates>
</xsl:template>

<xsl:template match="group">
<xsl:if test="position()=1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

With your input data (with the missing <?xml version=1.0"?> line
supplied), this XSLT produces:

<?xml version="1.0"?>
<group>
<sample>qwq</sample>
<sample>lala</sample>
<sample>lala2</sample>
</group>

Ed

 
Reply With Quote
 
 
 
 
R
Guest
Posts: n/a
 
      01-16-2005
well...
it is a small missunderstanding
I only needed the maximal count() of nodes inside all the 'group'
nodes, in my example the answer is 3.
That was all

Your XSLT transfomed them into new XML but how can it be modified so
that I can have 'the answer' in e.g. variable: <xsl:variable
name="Elements" select="..." />

thanks in advance for further explanations(just a newbie - sorry)
best regards R

 
Reply With Quote
 
Ed Beroset
Guest
Posts: n/a
 
      01-16-2005
R wrote:
> well...
> it is a small missunderstanding
> I only needed the maximal count() of nodes inside all the 'group'
> nodes, in my example the answer is 3.
> That was all
>
> Your XSLT transfomed them into new XML but how can it be modified so
> that I can have 'the answer' in e.g. variable: <xsl:variable
> name="Elements" select="..." />
>
> thanks in advance for further explanations(just a newbie - sorry)


No problem. I'm sorry I misunderstood your question. Here's a
modification to the originally posted XSLT:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnssd="http://www.w3.org/2001/XMLSchema"
>

<xslutput method="text" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="root/group">
<xsl:sort select="count(./*)" order="descending" />
</xsl:apply-templates>
</xsl:template>

<xsl:template match="group">
<xsl:if test="position()=1">
<xsl:value-of select="count(./*)"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

This one produces just "3" as output. The changes are only that the
output method is now text instead of xml, and instead of <xsl:copy-of
select="."/> it's now <xsl:value-of select="count(./*)"/>

Ed

 
Reply With Quote
 
Ed Beroset
Guest
Posts: n/a
 
      01-16-2005
Ed Beroset wrote:
> R wrote:
>
>> well...
>> it is a small missunderstanding
>> I only needed the maximal count() of nodes inside all the 'group'
>> nodes, in my example the answer is 3.
>> That was all
>>
>> Your XSLT transfomed them into new XML but how can it be modified so
>> that I can have 'the answer' in e.g. variable: <xsl:variable
>> name="Elements" select="..." />
>>
>> thanks in advance for further explanations(just a newbie - sorry)

>
>
> No problem. I'm sorry I misunderstood your question.


Rereading my answer, it could be that I've omitted a step which might
not be obvious to somebody new to XSLT; namely how to get a result
fragment into a variable. This is just a part of the XSLT, the rest is
as before:

<xsl:template match="/">
<xsl:variable name="Elements">
<xsl:apply-templates select="root/group">
<xsl:sort select="count(./*)" order="descending" />
</xsl:apply-templates>
</xsl:variable>
<xsl:text>The maximum depth is </xsl:text>
<xsl:value-of select="$Elements"/>
</xsl:template>

Ed

 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-16-2005
Ed Beroset wrote:

>>> it is a small missunderstanding
>>> I only needed the maximal count() of nodes inside all the 'group'
>>> nodes, in my example the answer is 3.
>>> That was all


I understood that an XSLT is sought, but I could
not resist and write a solution in XMLgawk:

BEGIN { XMLMODE=1 }
XMLSTARTELEM == "group" { count = 0 ; next }
XMLSTARTELEM { count ++ }
XMLENDELEM == "group" && count > max { max = count }
END { print max }

> Rereading my answer, it could be that I've omitted a step which might
> not be obvious to somebody new to XSLT; namely how to get a result
> fragment into a variable. This is just a part of the XSLT, the rest is
> as before:


What I learn from this is that XSLT has some subtle edges.

> <xsl:template match="/">
> <xsl:variable name="Elements">
> <xsl:apply-templates select="root/group">
> <xsl:sort select="count(./*)" order="descending" />
> </xsl:apply-templates>
> </xsl:variable>
> <xsl:text>The maximum depth is </xsl:text>
> <xsl:value-of select="$Elements"/>
> </xsl:template>


As you said, this is only part of your solution.
I am still unconvinced that it is a good idead
to have markup blocks in the source code of a
script language.
 
Reply With Quote
 
Ed Beroset
Guest
Posts: n/a
 
      01-17-2005
Jürgen Kahrs wrote:
> Ed Beroset wrote:
>
>
>> <xsl:template match="/">
>> <xsl:variable name="Elements">
>> <xsl:apply-templates select="root/group">
>> <xsl:sort select="count(./*)" order="descending" />
>> </xsl:apply-templates>
>> </xsl:variable>
>> <xsl:text>The maximum depth is </xsl:text>
>> <xsl:value-of select="$Elements"/>
>> </xsl:template>

>
>
> As you said, this is only part of your solution.


The whole solution is here; the difference is only a few lines:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnssd="http://www.w3.org/2001/XMLSchema"
>

<xslutput method="text" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="Elements">
<xsl:apply-templates select="root/group">
<xsl:sort select="count(./*)" order="descending" />
</xsl:apply-templates>
</xsl:variable>
<xsl:text>The maxiumum depth is </xsl:text>
<xsl:value-of select="$Elements"/>
</xsl:template>

<xsl:template match="group">
<xsl:if test="position()=1">
<xsl:value-of select="count(./*)"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

> I am still unconvinced that it is a good idea
> to have markup blocks in the source code of a
> script language.


Maybe it would help if instead you thought of it as markup language in
the middle of markup blocks. XSLT is not a script language!

Ed

 
Reply With Quote
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-17-2005
Ed Beroset wrote:

> Maybe it would help if instead you thought of it as markup language in
> the middle of markup blocks.


The problem for me is not so much the markup blocks and their nesting.
The problem for me is that the syntax is ugly, hard
to read and hard to debug.

> XSLT is not a script language!


OK, XSLT is not a scripting language.
But this property of XSLT (*not* being
a scripting language) limits its range
of application even further.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can anyone implement the pseudocode for the maximal rectangle problem ? Dan C Programming 1 03-26-2011 06:26 PM
Total maximal size of data Alexander Moibenko Python 11 01-28-2010 11:54 AM
Re: Total maximal size of data Terry Reedy Python 4 01-25-2010 08:56 PM
Maximal Cliques arush C++ 2 07-15-2009 04:58 AM
Powershot S40 CF Card -> maximal speed? Nicksur19 Digital Photography 0 10-23-2004 08:48 PM



Advertisments