Search for

B

barkeeper

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
 
P

Pavel Lepin

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

Joseph Kesselman

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]"/>
 
B

barkeeper

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.



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]"/>
 
J

Joseph Kesselman

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

Martin Honnen

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

barkeeper

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



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.
 

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
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top