Results 1 to 3 of 3

Thread: combination of Group and Sequence in xslt 2.0

  1. #1
    Join Date
    Aug 2006
    Posts
    332

    combination of Group and Sequence 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"
    xmlnssl="http://www.w3.org/1999/XSL/Transform"
    xmlnss="http://www.w3.org/2001/XMLSchema" >
    <xslutput 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 xmlnss="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 sequence of simple string values
    over which I want to iterate. (Please see attribute BaseName="otokarl" which indicates that too )

    WHAT IS WRONG HERE ?
    any help ?

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: combination of Group and Sequence in xslt 2.0

    he 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 () unction 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>

  3. #3
    Join Date
    Aug 2006
    Posts
    332

    Re: combination of Group and Sequence in xslt 2.0

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

Similar Threads

  1. Use of XSLT
    By Mithun Seth in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 01:16 PM
  2. How to Display XML with XSLT?
    By SKREECH in forum Software Development
    Replies: 4
    Last Post: 02-02-2010, 01:19 AM
  3. Is XSL and XSLT are same
    By Crespin in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 05:58 AM
  4. Group Policy Applying sequence and result
    By Invinceble in forum Active Directory
    Replies: 3
    Last Post: 18-02-2009, 02:35 AM
  5. problem with xml et xslt
    By manjava in forum Software Development
    Replies: 0
    Last Post: 28-12-2008, 05:26 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,111,458.81030 seconds with 16 queries