473,473 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Group and Sequence combination in xslt 2.0

Dear ALL,
I need some help on xsl:sequence. I'm using the Altova XSLT processor,
but I'm quite confident it is not an processor issue. It's probably my
bad knowledge of xslt 2.0.
I have probably put more that required in the test case, but it
basically covers my more complex code.

Here is the xml example source:
<?xml version="1.0" encoding="UTF-8"?>
<SeqTest>
<Item>otto_L</Item>
<Item>otto_L</Item>
<Item>otto_R</Item>
<Item>otto_L</Item>
<Item>karl_L</Item>
<Item>karl_R</Item>
<Item>nepumuk_L</Item>
</SeqTest>
My xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/
>
<xsl:template match="/">
<DEBUG>
<xsl:for-each select="SeqTest/Item">
<ForeEach Pos="{position()}" Name="{.}"/>
</xsl:for-each>
<xsl:variable name="vSeq">
<xsl:for-each-group select="SeqTest/Item"
group-by="substring(.,1,string-length(.)-2)">
<xsl:variable name="vBaseName" s
elect="substring(.,1,string-length(.)-2)"/>
<xsl:if test="$vBaseName != 'nepumuk'">
<xsl:sequence select="$vBaseName"/>
</xsl:if>
</xsl:for-each-group>
</xsl:variable>
<Seq>
<xsl:for-each select="$vSeq">
<Item BaseName="{.}"/>
</xsl:for-each>
</Seq>
</DEBUG>
</xsl:template>
</xsl:stylesheet>
The UNEXPECTED result
<?xml version="1.0" encoding="UTF-8"?>
<DEBUG xmlns:xs="http://www.w3.org/2001/XMLSchema">
<ForeEach Pos="1" Name="otto_L"/>
<ForeEach Pos="2" Name="otto_L"/>
<ForeEach Pos="3" Name="otto_R"/>
<ForeEach Pos="4" Name="otto_L"/>
<ForeEach Pos="5" Name="karl_L"/>
<ForeEach Pos="6" Name="karl_R"/>
<ForeEach Pos="7" Name="nepumuk_L"/>
<Seq>
<Item BaseName="ottokarl"/>
</Seq>
</DEBUG>

As you can see the result element Item is only once there.
For me it meas that I have not build a sequnce of simple string values
over which I want to iterate.
(Please see attribute BaseName="otokarl" which indictes that too )

WHAT IS WRONG HERE ?

Any help is welcome

Rolf
Oct 9 '08 #1
2 2609
RolfK wrote:
<xsl:variable name="vSeq">
<xsl:for-each-group select="SeqTest/Item"
group-by="substring(.,1,string-length(.)-2)">
<xsl:variable name="vBaseName" s
elect="substring(.,1,string-length(.)-2)"/>
<xsl:if test="$vBaseName != 'nepumuk'">
<xsl:sequence select="$vBaseName"/>
</xsl:if>
</xsl:for-each-group>
</xsl:variable>
<Seq>
<xsl:for-each select="$vSeq">
<Item BaseName="{.}"/>
</xsl:for-each>
</Seq>
<Seq>
<Item BaseName="ottokarl"/>
</Seq>
</DEBUG>

As you can see the result element Item is only once there.
For me it meas that I have not build a sequnce of simple string values
over which I want to iterate.
The variable is currently a temporary tree. If you want a sequence of
strings then you need to use the 'as' attribute as follows:
<xsl:variable name="vSeq" as="xs:string*">
<xsl:for-each-group select="SeqTest/Item"
group-by="substring(.,1,string-length(.)-2)">
<xsl:variable name="vBaseName"
select="substring(.,1,string-length(.)-2)"/>
<xsl:if test="$vBaseName != 'nepumuk'">
<xsl:sequence select="$vBaseName"/>
</xsl:if>
</xsl:for-each-group>
</xsl:variable>
Not related to your problem is the following suggestion: instead of
computing the "base name" again and again you can simply call the
current-grouping-key() function e.g.

<xsl:for-each-group select="SeqTest/Item"
group-by="substring(.,1,string-length(.)-2)">
<xsl:variable name="vBaseName" select="current-grouping-key()"/>
<xsl:if test="$vBaseName != 'nepumuk'">
<xsl:sequence select="$vBaseName"/>
</xsl:if>
</xsl:for-each-group>

or you could get rid of the variable completely:

<xsl:for-each-group select="SeqTest/Item"
group-by="substring(.,1,string-length(.)-2)">
<xsl:if test="current-grouping-key() != 'nepumuk'">
<xsl:sequence select="current-grouping-key()"/>
</xsl:if>
</xsl:for-each-group>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 9 '08 #2
On 9 Okt., 12:55, Martin Honnen <mahotr...@yahoo.dewrote:
RolfK wrote:
* * * * * * * * * *<xsl:variable name="vSeq">
* * * * * * * * * * * * * *<xsl:for-each-group select="SeqTest/Item"
* * * * * * * * * * * * * * * * * *group-by="substring(.,1,string-length(.)-2)">
* * * * * * * * * * * * * * * * * *<xsl:variable name="vBaseName" s
* * * * * * * * * * * * * * * * * *elect="substring(.,1,string-length(.)-2)"/>
* * * * * * * * * * * * * * * * * *<xsl:if test="$vBaseName != 'nepumuk'">
* * * * * * * * * * * * * * * * * ** * * *<xsl:sequence select="$vBaseName"/>
* * * * * * * * * * * * * * * * * *</xsl:if>
* * * * * * * * * * * * * *</xsl:for-each-group>
* * * * * * * * * *</xsl:variable>
* * * * * * * * * *<Seq>
* * * * * * * * * * * * * *<xsl:for-each select="$vSeq">
* * * * * * * * * * * * * * * * * *<Item BaseName="{.}"/>
* * * * * * * * * * * * * *</xsl:for-each>
* * * * * * * * * *</Seq>
* *<Seq>
* * * * * *<Item BaseName="ottokarl"/>
* *</Seq>
</DEBUG>
As you can see the result element Item is only once there.
For me it meas that I have not build a sequnce of simple string values
over which I want to iterate.

The variable is currently a temporary tree. If you want a sequence of
strings then you need to use the 'as' attribute as follows:

* * * * * * * * * * * * <xsl:variable name="vSeq" as="xs:string*">
* * * * * * * * * * * * * * * * <xsl:for-each-group select="SeqTest/Item"
* * * * * * * * * * * * * * * * * * * * group-by="substring(.,1,string-length(.)-2)">
* * * * * * * * * * * * * * * * * * * * <xsl:variable name="vBaseName"
select="substring(.,1,string-length(.)-2)"/>
* * * * * * * * * * * * * * * * * * * * <xsl:if test="$vBaseName != 'nepumuk'">
* * * * * * * * * * * * * * * * * * * * * * * * <xsl:sequence select="$vBaseName"/>
* * * * * * * * * * * * * * * * * * * * </xsl:if>
* * * * * * * * * * * * * * * * </xsl:for-each-group>
* * * * * * * * * * * * </xsl:variable>

Not related to your problem is the following suggestion: instead of
computing the "base name" again and again you can simply call the
current-grouping-key() function e.g.

<xsl:for-each-group select="SeqTest/Item"
* * * * * * * * * * * * * * * * * * * * group-by="substring(.,1,string-length(.)-2)">
* * * * * * * * * * * * * * * * * * * * <xsl:variable name="vBaseName" select="current-grouping-key()"/>
* * * * * * * * * * * * * * * * * * * * <xsl:if test="$vBaseName != 'nepumuk'">
* * * * * * * * * * * * * * * * * * * * * * * * <xsl:sequence select="$vBaseName"/>
* * * * * * * * * * * * * * * * * * * * </xsl:if>
* * * * * * * * * * * * * * * * </xsl:for-each-group>

or you could get rid of the variable completely:

<xsl:for-each-group select="SeqTest/Item"
* * * * * * * * * * * * * * * * * * * * group-by="substring(.,1,string-length(.)-2)">
* * * * * * * * * * * * * * * * * * * * <xsl:if test="current-grouping-key() != 'nepumuk'">
* * * * * * * * * * * * * * * * * * * * * * * * <xsl:sequence select="current-grouping-key()"/>
* * * * * * * * * * * * * * * * * * * * </xsl:if>
* * * * * * * * * * * * * * * * </xsl:for-each-group>
--

* * * * Martin Honnen
* * * *http://JavaScript.FAQTs.com/- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
Dear Martin,

Thanks a lot for this substancial and quick help !
I guess it will improve my code and even the key might inprove the
overall performance.

Rolf
Oct 9 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rick | last post by:
After being frustrated with this issue on several occasions I think I found the secret sauce for solving the issue after reading a few different messages about what others thought and trying a...
2
by: Filipe | last post by:
Hi all, This is a urgent situation. I´m counting on you guys to give some clue to get the right XSL Transformation. I need do group all nested tags <Entry> inside tag <Record> according to an...
1
by: Juho Jussila | last post by:
Hi How can I easily ensure that Xml document has elements in certain order? I think it can be done with Xml schema, but I'd like to use Xslt, because the validation is a part of Xslt...
0
by: Betty Harvey | last post by:
Happy 2006! I hope everyone had a great holiday and will have a good new year. The next meeting of the XML Users Group will be held on Wednesday, January 18, 2006 at the American Geophysical...
2
by: jon|k | last post by:
hi all-- i need to do a transformation that removes duplicates (among other things). to accomplish that, i'm trying to use for-each-group, but it doesn't work. i need to select for duplicates by...
0
by: Betty Harvey | last post by:
The next meeting of the XML Users Group will be held on Wednesday, February 15, 2006 at the American Geophysical Union (AGU) at 2000 Florida Avenue, N.W., Washington, DC 20009-1277. The meeting...
0
by: ombralonga | last post by:
Hi, I'm getting the following validation error with XMLspy when validating an "xsd" file: Schema Error: the group 'DateUnionGroup' is undefined However, in the xsd document the...
1
by: Gerben Abbink | last post by:
In Relax NG, I understand all other group operators (interleave, choice, oneOrMore, zeroOrMore, optional), but what is the purpose of <group>? Can you give an example?
2
by: jehugaleahsa | last post by:
Hello: I have an XML file that contains records with a Date attribute. I would like to group all the records together with a particular date, so that I can later iterate over the different dates...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.