xslt: continue in for-each-group?

B

bbembi_de

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
 
A

Alain Ketterlin

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.
 
P

Pavel Lepin

<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>
 
B

bbembi_de

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






<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>
 
M

Martin Honnen

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?
 
B

bbembi_de

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
 
J

Joseph Kesselman

You haven't shown your code, but this sounds like a simple matter of
rewriting the test...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top