0

Hi,

I have a multiple xslt process.

I would like to divide a group, for example the party that start between
00:00:00 - 10:00:00 called as "Group A"
10:00:00 - 14:00:00 will call as "Group B"
or 14:00:00 - 20:00:00 will call as "Group C"

and I would like to record the "number" attribute where is start and end the party_group element can be anywhere, doesn't have to be at the beginning.

I'm also not sure how to do if time gt than 10:00:00 and less than 14:00:00 :confused:

So I was thinking to create another variable including the "start" element within phase1, therefore I can group by "start" date after I created the "number" then remove "start" element to get the final result. Not sure how to remove the element though :confused:

Will this be possible to do? I'm confused how to keep the start and end number. I hope someone can give me some tips. Thanks so much

Here is my input

<parties>
    <events>
        <event id="a">
            <type>dance party</type>
            <title>80s</title>
            <start>2009-12-01T00:28:30</start>
        </event>
        <event id="b">
            <type>cocktail party</type>
            <title>Flower Garden</title>
            <start>2009-12-01T10:09:34</start>
        </event>
        <event id="c">
            <type>cocktail party</type>
            <title>Prewedding party</title>
            <start>2009-12-01T14:30:34</start>
        </event>
        <event id="d">
            <type>kids party</type>
            <title>Fairy Party</title>
            <start>2009-12-01T20:00:00</start>
        </event>
        <event id="e">
            <type>kids party</type>
            <title>Animals Party</title>
            <start>2009-12-01T20:05:00</start>
        </event>
    </events>
</parties>

I would like my final result look like this

<mypartiescollection Version="1.1">
   <party_group name="Group A" fromNumber="1" toNumber="1"/>
    <party_group name="Group B" fromNumber="2" toNumber="3"/>
   <party_group name="Group C" fromNumber="4" toNumber="6"/>
    <dance_party number="1" id="a">
        <title>80s</title>
    </dance_party>
    <cocktail_party number="2" id="b">
        <title>Flower Garden</title>
    </cocktail_party>
    <cocktail_party number="3" id="c">
        <title>Prewedding party</title>
    </cocktail_party>
    <comment message="my comment" number="4"/>
    <kids_party number="5" id="d">
        <title>Fairy Party</title>
    </kids_party>
<comment message="my comment" number="5"/>
    <kids_party number="6" id="e">
        <title>Animals Party</title>
    </kids_party>
</mypartiescollection>

I have a multi phase xslt, the reason is so that I can put the number.

phase1.xslt

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/" mode="phase1">
                <xsl:element name="mypartiescollection">
                       <xsl:attribute name="Version">1.1</xsl:attribute>
           <xsl:apply-templates/>
                </xsl:element>
    </xsl:template>
    
        <xsl:template match="event">
       <xsl:if test="type='dance party'">
          <xsl:element name="dance_party">
          <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                          <xsl:element name="title">
               <xsl:value-of select="title"/>
                          </xsl:element>
                <xsl:element name="start">
                    <xsl:value-of select='start'/>
                </xsl:element>
            </xsl:element>
        </xsl:if>
        <xsl:if test="type = 'cocktail party'">
            <xsl:element name="cocktail_party">
                <xsl:attribute name="id"><xsl:value-of select='@id'></xsl:value-of></xsl:attribute>
                <xsl:element name="title">
                    <xsl:value-of select='title'/>
                </xsl:element>
                <xsl:element name="start">
                    <xsl:value-of select='start'/>
                </xsl:element>
            </xsl:element>
        </xsl:if>
        <xsl:if test="type = 'kids party'">
<xsl:element name="comment>
     <xsl:attribute name="message"/>my comment</xsl:attribute>
</xsl:element>
            <xsl:element name="kids_party">
                <xsl:attribute name="id"><xsl:value-of select='@id'></xsl:value-of></xsl:attribute>
                <xsl:element name="title">
                    <xsl:value-of select='title'/>
                </xsl:element>     
                <xsl:element name="start">
                    <xsl:value-of select='start'/>
                </xsl:element>
            </xsl:element>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

my phase 1 result before having "number" attribute

<mypartiescollection Version="1.1">
    <dance_party id="a">
        <title>80s</title>
        <start>2009-12-01T00:28:30</start>
    </dance_party>
    <cocktail_party id="b">
        <title>Flower Garden</title>
        <start>2009-12-01T10:09:34</start>
    </cocktail_party>
    <cocktail_party id="c">
        <title>Prewedding party</title>
        <start>2009-12-01T14:30:34</start>
    </cocktail_party>
    <comment message="my comment" />
    <kids_party number="5" id="d">
        <title>Fairy Party</title>
        <start>2009-12-01T20:00:00</start>
   </kids_party>
   <comment message="my comment"/>
    <kids_party number="7" id="e">
        <title>Animals Party</title> 
        <start>2009-12-01T20:05:00</start>
    </kids_party>
</mypartiescollection>

phase2.xslt

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:import href="test.xslt"/>
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:variable name="data">
        <xsl:apply-templates select="/" mode="phase1"/>
    </xsl:variable>
    
    <xsl:template match="/">
        <xsl:variable name="data2">
        <xsl:apply-templates select="$data" mode="phase2"/>
    </xsl:variable>        
        <xsl:apply-templates select="$data2" mode="group"/>
    </xsl:template>

    <xsl:template match="/" mode="group">
        <xsl:for-each-group select="//dance_party|//cocktail_party|//kids_party" group-by="f:party-group(xs:time(xs:dateTime(start)))"> 

    
<!-- I'm not so sure about this part. somehow this part have error of "An empty sequence is not allowed as the first argument of f:party_group() -->
              <party_group name="{current-grouping-key()}"
                      fromNumber="{min(current-group()/number)}"
                       toNumber="{max(current-group()/number)}"
                       />
        </xsl:for-each-group>
    </xsl:template>

    
    <xsl:function name="f:party-group" as="xs:string">
       <xsl:param name="start-time" as="xs:time"/>
       <xsl:sequence select="if ($start-time gt xs:time('10:00:00')) then 'Group B' else 'Group A'"/>
       <!-- I'm also not sure how to do if time gt than 10:00:00 and less than 14:00:00 -->
    </xsl:function>
    
    <xsl:template match="mypartiescollection" mode="phase2">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()" mode="phase2"/>
    </xsl:copy>
    </xsl:template>
    
 <xsl:template match="dance_party|cocktail_party|kids_party" mode="phase2" name="party_collection">
  <xsl:copy>
    <xsl:attribute name="number">
       <xsl:number count="dance_party|cocktail_party|kids_party"/>
    </xsl:attribute>
    <xsl:copy-of select="@* | node()"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

data2 result. after I generate the number.

<mypartiescollection Version="1.1">
    <dance_party number="1" id="a">
        <title>80s</title>
        <start>2009-12-01T00:28:30</start>
    </dance_party>
    <cocktail_party number="2" id="b">
        <title>Flower Garden</title>
        <start>2009-12-01T13:09:34</start>
    </cocktail_party>
    <cocktail_party number="3" id="c">
        <title>Prewedding party</title>
        <start>2009-12-01T13:30:34</start>
    </cocktail_party>
    <comment message="my comment" number="4" />
    <kids_party number="5" id="d">
        <title>Fairy Party</title>
        <start>2009-12-01T20:00:00</start>
   </kids_party>
   <comment message="my comment" number="6"/>
    <kids_party number="7" id="e">
        <title>Animals Party</title> 
        <start>2009-12-01T20:05:00</start>
    </kids_party>
</mypartiescollection>

Edited by geek.shrek: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by yuvanbala
0

Hi Dear,

First, you have to separate the event timing as <start year="2009" month="12" day="01" hour="00" min="28" sec="30"/> instead of <start>2009-12-01T00:28:30</start>.

Then you use sorting the tag based on the timings. Try this.

Regards,
yuvanbala

Hi,

I have a multiple xslt process.

I would like to divide a group, for example the party that start between
00:00:00 - 10:00:00 called as "Group A"
10:00:00 - 14:00:00 will call as "Group B"
or 14:00:00 - 20:00:00 will call as "Group C"

and I would like to record the "number" attribute where is start and end the party_group element can be anywhere, doesn't have to be at the beginning.

I'm also not sure how to do if time gt than 10:00:00 and less than 14:00:00 :confused:

So I was thinking to create another variable including the "start" element within phase1, therefore I can group by "start" date after I created the "number" then remove "start" element to get the final result. Not sure how to remove the element though :confused:

Will this be possible to do? I'm confused how to keep the start and end number. I hope someone can give me some tips. Thanks so much

Here is my input

<parties>
    <events>
        <event id="a">
            <type>dance party</type>
            <title>80s</title>
            <start>2009-12-01T00:28:30</start>
        </event>
        <event id="b">
            <type>cocktail party</type>
            <title>Flower Garden</title>
            <start>2009-12-01T10:09:34</start>
        </event>
        <event id="c">
            <type>cocktail party</type>
            <title>Prewedding party</title>
            <start>2009-12-01T14:30:34</start>
        </event>
        <event id="d">
            <type>kids party</type>
            <title>Fairy Party</title>
            <start>2009-12-01T20:00:00</start>
        </event>
        <event id="e">
            <type>kids party</type>
            <title>Animals Party</title>
            <start>2009-12-01T20:05:00</start>
        </event>
    </events>
</parties>

I would like my final result look like this

<mypartiescollection Version="1.1">
   <party_group name="Group A" fromNumber="1" toNumber="1"/>
    <party_group name="Group B" fromNumber="2" toNumber="3"/>
   <party_group name="Group C" fromNumber="4" toNumber="6"/>
    <dance_party number="1" id="a">
        <title>80s</title>
    </dance_party>
    <cocktail_party number="2" id="b">
        <title>Flower Garden</title>
    </cocktail_party>
    <cocktail_party number="3" id="c">
        <title>Prewedding party</title>
    </cocktail_party>
    <comment message="my comment" number="4"/>
    <kids_party number="5" id="d">
        <title>Fairy Party</title>
    </kids_party>
<comment message="my comment" number="5"/>
    <kids_party number="6" id="e">
        <title>Animals Party</title>
    </kids_party>
</mypartiescollection>

I have a multi phase xslt, the reason is so that I can put the number.

phase1.xslt

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/" mode="phase1">
                <xsl:element name="mypartiescollection">
                       <xsl:attribute name="Version">1.1</xsl:attribute>
           <xsl:apply-templates/>
                </xsl:element>
    </xsl:template>
    
        <xsl:template match="event">
       <xsl:if test="type='dance party'">
          <xsl:element name="dance_party">
          <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                          <xsl:element name="title">
               <xsl:value-of select="title"/>
                          </xsl:element>
                <xsl:element name="start">
                    <xsl:value-of select='start'/>
                </xsl:element>
            </xsl:element>
        </xsl:if>
        <xsl:if test="type = 'cocktail party'">
            <xsl:element name="cocktail_party">
                <xsl:attribute name="id"><xsl:value-of select='@id'></xsl:value-of></xsl:attribute>
                <xsl:element name="title">
                    <xsl:value-of select='title'/>
                </xsl:element>
                <xsl:element name="start">
                    <xsl:value-of select='start'/>
                </xsl:element>
            </xsl:element>
        </xsl:if>
        <xsl:if test="type = 'kids party'">
<xsl:element name="comment>
     <xsl:attribute name="message"/>my comment</xsl:attribute>
</xsl:element>
            <xsl:element name="kids_party">
                <xsl:attribute name="id"><xsl:value-of select='@id'></xsl:value-of></xsl:attribute>
                <xsl:element name="title">
                    <xsl:value-of select='title'/>
                </xsl:element>     
                <xsl:element name="start">
                    <xsl:value-of select='start'/>
                </xsl:element>
            </xsl:element>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

my phase 1 result before having "number" attribute

<mypartiescollection Version="1.1">
    <dance_party id="a">
        <title>80s</title>
        <start>2009-12-01T00:28:30</start>
    </dance_party>
    <cocktail_party id="b">
        <title>Flower Garden</title>
        <start>2009-12-01T10:09:34</start>
    </cocktail_party>
    <cocktail_party id="c">
        <title>Prewedding party</title>
        <start>2009-12-01T14:30:34</start>
    </cocktail_party>
    <comment message="my comment" />
    <kids_party number="5" id="d">
        <title>Fairy Party</title>
        <start>2009-12-01T20:00:00</start>
   </kids_party>
   <comment message="my comment"/>
    <kids_party number="7" id="e">
        <title>Animals Party</title> 
        <start>2009-12-01T20:05:00</start>
    </kids_party>
</mypartiescollection>

phase2.xslt

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:import href="test.xslt"/>
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:variable name="data">
        <xsl:apply-templates select="/" mode="phase1"/>
    </xsl:variable>
    
    <xsl:template match="/">
        <xsl:variable name="data2">
        <xsl:apply-templates select="$data" mode="phase2"/>
    </xsl:variable>        
        <xsl:apply-templates select="$data2" mode="group"/>
    </xsl:template>

    <xsl:template match="/" mode="group">
        <xsl:for-each-group select="//dance_party|//cocktail_party|//kids_party" group-by="f:party-group(xs:time(xs:dateTime(start)))"> 

    
<!-- I'm not so sure about this part. somehow this part have error of "An empty sequence is not allowed as the first argument of f:party_group() -->
              <party_group name="{current-grouping-key()}"
                      fromNumber="{min(current-group()/number)}"
                       toNumber="{max(current-group()/number)}"
                       />
        </xsl:for-each-group>
    </xsl:template>

    
    <xsl:function name="f:party-group" as="xs:string">
       <xsl:param name="start-time" as="xs:time"/>
       <xsl:sequence select="if ($start-time gt xs:time('10:00:00')) then 'Group B' else 'Group A'"/>
       <!-- I'm also not sure how to do if time gt than 10:00:00 and less than 14:00:00 -->
    </xsl:function>
    
    <xsl:template match="mypartiescollection" mode="phase2">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()" mode="phase2"/>
    </xsl:copy>
    </xsl:template>
    
 <xsl:template match="dance_party|cocktail_party|kids_party" mode="phase2" name="party_collection">
  <xsl:copy>
    <xsl:attribute name="number">
       <xsl:number count="dance_party|cocktail_party|kids_party"/>
    </xsl:attribute>
    <xsl:copy-of select="@* | node()"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

data2 result. after I generate the number.

<mypartiescollection Version="1.1">
    <dance_party number="1" id="a">
        <title>80s</title>
        <start>2009-12-01T00:28:30</start>
    </dance_party>
    <cocktail_party number="2" id="b">
        <title>Flower Garden</title>
        <start>2009-12-01T13:09:34</start>
    </cocktail_party>
    <cocktail_party number="3" id="c">
        <title>Prewedding party</title>
        <start>2009-12-01T13:30:34</start>
    </cocktail_party>
    <comment message="my comment" number="4" />
    <kids_party number="5" id="d">
        <title>Fairy Party</title>
        <start>2009-12-01T20:00:00</start>
   </kids_party>
   <comment message="my comment" number="6"/>
    <kids_party number="7" id="e">
        <title>Animals Party</title> 
        <start>2009-12-01T20:05:00</start>
    </kids_party>
</mypartiescollection>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.