xslt: continue in for-each-group?

Discussion in 'XML' started by bbembi_de@lycos.de, Feb 5, 2008.

  1. Guest

    Hello everyone,

    I have just a little xslt problem:

    <xsl:for-each-group select="item" group-by="name">
    <xsl:if test="contains(name, 'teststring11')">
    <xsl:apply-templates select="current-group()"/>
    </xsl:if>
    <xsl:if test="contains(name, 'teststring22')">
    <xsl:apply-templates select="current-group()"/>
    </xsl:if>
    </xsl:for-each-group>

    This xslt is fine, but has 1 problem: I want to make a "continue" in
    my if clauses. If the program walks into a if clause it should go to
    the next for-each-group. I don't want every if clause to be executed,
    only one.

    How can I do that?

    Thanks very much.

    bye bembi
     
    , Feb 5, 2008
    #1
    1. Advertising

  2. "" <> writes:

    > I have just a little xslt problem:
    >
    > <xsl:for-each-group select="item" group-by="name">
    > <xsl:if test="contains(name, 'teststring11')">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:if>
    > <xsl:if test="contains(name, 'teststring22')">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:if>
    > </xsl:for-each-group>
    >
    > This xslt is fine, but has 1 problem: I want to make a "continue" in
    > my if clauses. If the program walks into a if clause it should go to
    > the next for-each-group. I don't want every if clause to be executed,
    > only one.
    >
    > How can I do that?


    <xsl:choose> ?

    -- Alain.
     
    Alain Ketterlin, Feb 5, 2008
    #2
    1. Advertising

  3. Pavel Lepin Guest

    <> wrote in
    <>:
    > <xsl:for-each-group select="item" group-by="name">
    > <xsl:if test="contains(name, 'teststring11')">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:if>
    > <xsl:if test="contains(name, 'teststring22')">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:if>
    > </xsl:for-each-group>
    >
    > This xslt is fine, but has 1 problem: I want to make a
    > "continue" in my if clauses. If the program walks into a
    > if clause it should go to the next for-each-group. I don't
    > want every if clause to be executed, only one.


    While <xsl:choose/> is clearly an option, you might want to
    consider the fact that specifying templates matching your
    groups is often a better option. Your example seems to be
    equivalent to:

    <xsl:for-each-group select="item" group-by="name">
    <xsl:apply-templates select="current-group()"/>
    </xsl:for-each-group>

    ....but assuming you meant something like:

    <xsl:for-each-group select="item" group-by="name">
    <xsl:if test="contains(name,'teststring11')">
    <xsl:apply-templates select="current-group()"
    mode="mode-11"/>
    </xsl:if>
    <xsl:if test="contains(name,'teststring22')">
    <xsl:apply-templates select="current-group()"
    mode="mode-22"/>
    </xsl:if>
    </xsl:for-each-group>

    ....you might redesign that using just one template
    application and properly matching templates, such as:

    <xsl:template match="item[contains(name,'teststring11')]>
    <Stuff-11/>
    </xsl:template>
    <xsl:template match="item[contains(name,'teststring22')]>
    <Stuff-22/>
    </xsl:template>

    --
    When all you have is a transformation engine, everything
    looks like a tree.
     
    Pavel Lepin, Feb 5, 2008
    #3
  4. Guest

    Thanks very much for the info.

    Now I found out what my real problem is (I tried xsl:choose before
    writing the first mail):

    I have a xml like this:

    <root>
    <item name="111">
    <subitem name="aaa">
    <subitem name="bbb">
    </item>....

    I group the subitems but only want to output a item name once.
    I want only one item output.

    <xsl:for-each-group select="subitem" group-by="name">
    <xsl:apply-templates select="current-group()" //here I output
    the name of the parent (item) but this should be only once for every
    item.

    bye bembi






    On 5 Feb., 13:10, Pavel Lepin <> wrote:
    > <> wrote in
    > <>:
    >
    > > <xsl:for-each-group select="item" group-by="name">
    > > <xsl:if test="contains(name, 'teststring11')">
    > > <xsl:apply-templates select="current-group()"/>
    > > </xsl:if>
    > > <xsl:if test="contains(name, 'teststring22')">
    > > <xsl:apply-templates select="current-group()"/>
    > > </xsl:if>
    > > </xsl:for-each-group>

    >
    > > This xslt is fine, but has 1 problem: I want to make a
    > > "continue" in my if clauses. If the program walks into a
    > > if clause it should go to the next for-each-group. I don't
    > > want every if clause to be executed, only one.

    >
    > While <xsl:choose/> is clearly an option, you might want to
    > consider the fact that specifying templates matching your
    > groups is often a better option. Your example seems to be
    > equivalent to:
    >
    > <xsl:for-each-group select="item" group-by="name">
    > <xsl:apply-templates select="current-group()"/>
    > </xsl:for-each-group>
    >
    > ...but assuming you meant something like:
    >
    > <xsl:for-each-group select="item" group-by="name">
    > <xsl:if test="contains(name,'teststring11')">
    > <xsl:apply-templates select="current-group()"
    > mode="mode-11"/>
    > </xsl:if>
    > <xsl:if test="contains(name,'teststring22')">
    > <xsl:apply-templates select="current-group()"
    > mode="mode-22"/>
    > </xsl:if>
    > </xsl:for-each-group>
    >
    > ...you might redesign that using just one template
    > application and properly matching templates, such as:
    >
    > <xsl:template match="item[contains(name,'teststring11')]>
    > <Stuff-11/>
    > </xsl:template>
    > <xsl:template match="item[contains(name,'teststring22')]>
    > <Stuff-22/>
    > </xsl:template>
    >
    > --
    > When all you have is a transformation engine, everything
    > looks like a tree.
     
    , Feb 6, 2008
    #4
  5. Joseph Kesselman, Feb 7, 2008
    #5
  6. wrote:
    > Thanks very much for the info.
    >
    > Now I found out what my real problem is (I tried xsl:choose before
    > writing the first mail):
    >
    > I have a xml like this:
    >
    > <root>
    > <item name="111">
    > <subitem name="aaa">
    > <subitem name="bbb">
    > </item>....
    >
    > I group the subitems but only want to output a item name once.
    > I want only one item output.
    >
    > <xsl:for-each-group select="subitem" group-by="name">
    > <xsl:apply-templates select="current-group()" //here I output
    > the name of the parent (item) but this should be only once for every
    > item.


    I am not sure I understand what you want to achieve, your sample has
    just one item element having two subitem children with an attribute
    named 'name' which has different values so there is not much to group
    by. And your XPath does group-by="name", if you want to group on the
    attribute you need group-by="@name".
    Can you show us enough XML sample data that it becomes clear what you
    want to group?

    --

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

    Hello again,

    given the following xml:
    <root>
    <item name="111">
    <subitem name="aaa">
    <subitem name="bbb">
    <subitem name="ccc">
    </item>
    <item name="222">
    <subitem name="ccc">
    <subitem name="bbb">
    </item>

    If i want all item names that have the subitems aaa and ccc I want the
    output:
    111

    but currently I get the output:
    111
    111

    How can I do this?

    thanks bembi




    On 7 Feb., 17:12, Martin Honnen <> wrote:
    > wrote:
    > > Thanks very much for the info.

    >
    > > Now I found out what my real problem is (I triedxsl:choose before
    > > writing the first mail):

    >
    > > I have a xml like this:

    >
    > > <root>
    > > <item name="111">
    > > <subitem name="aaa">
    > > <subitem name="bbb">
    > > </item>....

    >
    > > I group the subitems but only want to output a item name once.
    > > I want only one item output.

    >
    > > <xsl:for-each-group select="subitem" group-by="name">
    > > <xsl:apply-templates select="current-group()" //here I output
    > > the name of the parent (item) but this should be only once for every
    > > item.

    >
    > I am not sure I understand what you want to achieve, your sample has
    > just one item element having two subitem children with an attribute
    > named 'name' which has different values so there is not much to group
    > by. And your XPath does group-by="name", if you want to group on the
    > attribute you need group-by="@name".
    > Can you show us enough XML sample data that it becomes clear what you
    > want to group?
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
     
    , Feb 11, 2008
    #7
  8. You haven't shown your code, but this sounds like a simple matter of
    rewriting the test...

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Feb 11, 2008
    #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. John Blair
    Replies:
    1
    Views:
    421
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    441
    Tjerk Wolterink
    Nov 3, 2004
  3. jon|k
    Replies:
    2
    Views:
    9,751
    jon|k
    Jan 20, 2006
  4. Pat Maddox
    Replies:
    6
    Views:
    161
    Marcin Mielżyński
    Jan 20, 2006
  5. Igor Nn
    Replies:
    7
    Views:
    445
    Johnny Morrice
    May 28, 2011
Loading...

Share This Page