xslt: continue in for-each-group?

bbembi_de

Usenet Archive
Joined
Nov 28, 2005
Messages
29
Reaction score
0
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
 

Alain Ketterlin

Usenet Archive
Joined
Apr 27, 2000
Messages
401
Reaction score
0
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.
 

Pavel Lepin

Usenet Archive
Joined
Apr 20, 2007
Messages
263
Reaction score
0
<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>
 

bbembi_de

Usenet Archive
Joined
Nov 28, 2005
Messages
29
Reaction score
0
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>
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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?
 

bbembi_de

Usenet Archive
Joined
Nov 28, 2005
Messages
29
Reaction score
0
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
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top