XSLT: selecting the maximal from all counts(newbie)

Discussion in 'XML' started by R, Jan 16, 2005.

  1. R

    R Guest

    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
     
    R, Jan 16, 2005
    #1
    1. Advertising

  2. R

    Ed Beroset Guest

    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"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    >

    <xsl:eek:utput 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
     
    Ed Beroset, Jan 16, 2005
    #2
    1. Advertising

  3. R

    R Guest

    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
     
    R, Jan 16, 2005
    #3
  4. R

    Ed Beroset Guest

    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"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    >

    <xsl:eek:utput 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
     
    Ed Beroset, Jan 16, 2005
    #4
  5. R

    Ed Beroset Guest

    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
     
    Ed Beroset, Jan 16, 2005
    #5
  6. 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.
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 16, 2005
    #6
  7. R

    Ed Beroset Guest

    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"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    >

    <xsl:eek:utput 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
     
    Ed Beroset, Jan 17, 2005
    #7
  8. 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.
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 17, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Bethard

    selecting dictionaries to maximize counts

    Steven Bethard, Feb 18, 2005, in forum: Python
    Replies:
    5
    Views:
    307
    Brian Beck
    Feb 19, 2005
  2. arush

    Maximal Cliques

    arush, Jul 14, 2009, in forum: C++
    Replies:
    2
    Views:
    515
    red floyd
    Jul 15, 2009
  3. Alexander Moibenko

    Total maximal size of data

    Alexander Moibenko, Jan 25, 2010, in forum: Python
    Replies:
    11
    Views:
    409
    denis
    Jan 28, 2010
  4. Terry Reedy

    Re: Total maximal size of data

    Terry Reedy, Jan 25, 2010, in forum: Python
    Replies:
    4
    Views:
    297
    Diez B. Roggisch
    Jan 25, 2010
  5. Dan
    Replies:
    1
    Views:
    593
    Öö Tiib
    Mar 26, 2011
Loading...

Share This Page