Search for

Discussion in 'XML' started by barkeeper@jubii.de, Feb 11, 2008.

  1. Guest

    Hi,

    I have a xml input file like this:

    <root>
    <element>
    <name>elem1</name>
    <child>
    <name>1</name>
    </child>
    <child>
    <name>2</name>
    </child>
    </element>
    <element>
    <name>elem2</name>
    <child>
    <name>2</name>
    </child>
    <child>
    <name>3</name>
    </child>
    </element>

    Now I want apply a template on every element element that has a child
    with name="3" or name="1" for example.

    I do this right now:
    <xsl:for-each-group select="root/elment" group-by="name">
    <xsl:if test="contains(current()/child/name, '3') or
    contains(current()/child/name, '1')" >
    <xsl:apply-templates select="current-group()"/>
    </xsl:if>
    </xsl:for-each-group>

    But I get a "too many items" error. I guess because there are more
    than one child nodes in a elment node.

    Thanks
    Peter
     
    , Feb 11, 2008
    #1
    1. Advertising

  2. Pavel Lepin Guest

    <> wrote in
    <>:
    > <root>
    > <element>
    > <name>elem1</name>
    > <child>
    > <name>1</name>
    > </child>
    > <child>
    > <name>2</name>
    > </child>
    > </element>
    > <element>
    > <name>elem2</name>
    > <child>
    > <name>2</name>
    > </child>
    > <child>
    > <name>3</name>
    > </child>
    > </element>


    Not well-formed.

    > Now I want apply a template on every element element that
    > has a child with name="3" or name="1" for example.


    <xsl:apply-templates
    select="/root/element[child/name/text()='1' or
    child/name/text()='3']"/>

    > <xsl:for-each-group select="root/elment" group-by="name">
    > <xsl:if test="contains(current()/child/name, '3') or
    > contains(current()/child/name, '1')" >
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:if>
    > </xsl:for-each-group>


    I think I'll just add xsl:for-each-group to my list of Evil
    Stuff That Confuses Neophytes.

    --
    When all you have is a transformation engine, everything
    looks like a tree.
     
    Pavel Lepin, Feb 11, 2008
    #2
    1. Advertising

  3. wrote:
    > Now I want apply a template on every element element that has a child
    > with name="3" or name="1"


    <xsl:apply-templates select="/root/element[child/name=3 or child/name=1]"/>

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Feb 11, 2008
    #3
  4. Guest

    Well this works, but not like I need it.
    I should have mentioned what I do in my template that I apply.

    I want to output the element names but only once the same.
    With the current solution I get the element names many times, to be
    exactly once for every child that's name is 1 or 3.

    That is why i grouped the names in a for-each-group loop.



    On 11 Feb., 16:22, Joseph Kesselman <>
    wrote:
    > wrote:
    > > Now I want apply a template on every element element that has a child
    > > with name="3" or name="1"

    >
    > <xsl:apply-templates select="/root/element[child/name=3 or child/name=1]"/>
    >
    > --
    > Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    , Feb 11, 2008
    #4
  5. wrote:
    > I want to output the element names but only once the same.


    See the XSLT FAQ website's "grouping" page; that should give you some
    useful techniques.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Feb 11, 2008
    #5
  6. wrote:
    > Well this works, but not like I need it.
    > I should have mentioned what I do in my template that I apply.
    >
    > I want to output the element names but only once the same.
    > With the current solution I get the element names many times, to be
    > exactly once for every child that's name is 1 or 3.
    >
    > That is why i grouped the names in a for-each-group loop.


    I think it is better you provide a sample of your XML that makes it
    clear what you need to group on and then describe the result you want to
    create with your stylesheet.
    So far we can only guess what you want, here is my attempt at guessing

    <xsl:for-each-group select="root/element[child/name = '1' or
    child/name = '3']" group-by="name">
    <xsl:value-of select="current-grouping-key()"/>
    <xsl:if test="position() != last()">
    <xsl:text> </xsl:text>
    </xsl:if>
    </xsl:for-each-group>

    It outputs the grouping key of each group.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 11, 2008
    #6
  7. Guest

    Hi folks,

    that was the clue:
    <xsl:for-each-group select="root/element[child/name = '1' ....

    Now it works.
    Thanks very much!

    I really have my problems with this xslt stuff :)

    Bye Peter



    On 11 Feb., 17:48, Martin Honnen <> wrote:
    > wrote:
    > > Well this works, but not like I need it.
    > > I should have mentioned what I do in my template that I apply.

    >
    > > I want to output the element names but only once the same.
    > > With the current solution I get the element names many times, to be
    > > exactly once for every child that's name is 1 or 3.

    >
    > > That is why i grouped the names in a for-each-group loop.

    >
    > I think it is better you provide a sample of your XML that makes it
    > clear what you need to group on and then describe the result you want to
    > create with your stylesheet.
    > So far we can only guess what you want, here is my attempt at guessing
    >
    > <xsl:for-each-group select="root/element[child/name = '1' or
    > child/name = '3']" group-by="name">
    > <xsl:value-of select="current-grouping-key()"/>
    > <xsl:if test="position() != last()">
    > <xsl:text> </xsl:text>
    > </xsl:if>
    > </xsl:for-each-group>
    >
    > It outputs the grouping key of each group.
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    , Feb 11, 2008
    #7
    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. =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=

    Google search result like site search!! How?

    =?Utf-8?B?TGFrc2htaSBOYXJheWFuYW4uUg==?=, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    715
    Lucas Tam
    May 6, 2005
  2. Andy
    Replies:
    1
    Views:
    374
    Jack Klein
    Nov 25, 2003
  3. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    618
    Anand Pillai
    Oct 15, 2003
  4. mason66
    Replies:
    0
    Views:
    437
    mason66
    Jul 27, 2006
  5. Abby Lee
    Replies:
    5
    Views:
    476
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page