Re: [xsl] build group of elements

Subject: Re: [xsl] build group of elements
From: henry human <henry_human@xxxxxxxx>
Date: Mon, 25 Jun 2012 14:26:36 +0100 (BST)
here is the xslt and the input data
------------------------Output------------------------------
<xsl:for-each
select="DATA/REF">
						
	<xsl:variable name="adrTyp" select="@ref:ADRTYP"/>
<xsl:variable name="refs" select="REF"/>
	<xsl:variable name="noOfSegments"
select="xs:integer(max(( '1', xs:string(ceiling(count($refs) div 5)) )))"/>
<xsl:for-each select="1 to $noOfSegments">
		<xsl:variable name="hope"
select="position() * 4"/>
		<L5Segment>
			<xsl:if test="($refs/CDE)[$hope -
4]">
				<_1st_Address_type_Code>
					<xsl:value-of select="$adrTyp"/>
</_1st_Address_type_Code>
				<_1st_Reference_Code>
					<xsl:value-of
select="($refs/CDE)[$hope - 4]"/>
				</_1st_Reference_Code>
<_1st_Reference>
					<xsl:value-of select="($refs/VAL)[$hope - 4]"/>
</_1st_Reference>
			</xsl:if>
			<xsl:if test="($refs/CDE)[$hope - 3]">
<_2nd_Address_type>
					<xsl:value-of select="$adrTyp"/>
</_2nd_Address_type>
				<_2nd_Reference_Code>
					<xsl:value-of
select="($refs/CDE)[$hope - 3]"/>
				</_2nd_Reference_Code>
<_2nd_Reference>
					<xsl:value-of select="($refs/VAL)[$hope - 3]"/>
</_2nd_Reference>
			</xsl:if>
			<xsl:if test="($refs/CDE)[$hope - 2]">
<_3rd_Address_type>
					<xsl:value-of select="$adrTyp"/>
</_3rd_Address_type>
				<_3rd_Reference_Code>
					<xsl:value-of
select="($refs/CDE)[$hope - 2]"/>
				</_3rd_Reference_Code>
<_3rd_Reference>
					<xsl:value-of select="($refs/VAL)[$hope - 2]"/>
</_3rd_Reference>
			</xsl:if>
			<xsl:if test="($refs/CDE)[$hope - 1]">
<_4th_Address_type>
					<xsl:value-of select="$adrTyp"/>
</_4th_Address_type>
				<_4th_Reference_Code>
					<xsl:value-of
select="($refs/CDE)[$hope - 1]"/>
				</_4th_Reference_Code>
<_4th_Reference>
					<xsl:value-of select="($refs/VAL)[$hope - 1]"/>
</_4th_Reference>
			</xsl:if>

		</L5Segment>
	</xsl:for-each>
</xsl:for-each>


--------------Input---------------
<?xml version="1.0"
encoding="UTF-8"?>
	<DATA>		
		
		<ref:REF ref:ADRTYP="FF">
			<CDE>SRN</CDE>
<VAL>4405050008759</VAL>
		</ref:REF>
		<ref:REF ref:ADRTYP="MM">
<CDE>AAA</CDE>
			<VAL>3378781118759</VAL>
		</ref:REF>
		<ref:REF
ref:ADRTYP="NN">
			<CDE>BBB</CDE>
			<VAL>2209094446927</VAL>
		</ref:REF>
<ref:REF ref:ADRTYP="BB">
			<CDE>BBB</CDE>
			<VAL>4405099999999</VAL>
</ref:REF>
		<ref:REF ref:ADRTYP="CC">
			<CDE>CCC</CDE>
<VAL>4444444418759</VAL>
		</ref:REF>
		<ref:REF ref:ADRTYP="DD">
<CDE>DDD</CDE>
			<VAL>0000000046927</VAL>
		</ref:REF>
		<ref:REF
ref:ADRTYP="EE">
			<CDE>EEE</CDE>
			<VAL>2020202020202</VAL>
		</ref:REF>
<ref:REF ref:ADRTYP="GG">
			<CDE>GGG</CDE>
			<VAL>667676767676</VAL>
</ref:REF>
		<ref:REF ref:ADRTYP="JJ">
			<CDE>JJJ</CDE>
<VAL>2828282828282</VAL>
		</ref:REF>
	</DATA>

--- G. Ken Holman
<gkholman@xxxxxxxxxxxxxxxxxxxx> schrieb am So, 24.6.2012:

> Von: G. Ken
Holman <gkholman@xxxxxxxxxxxxxxxxxxxx>
> Betreff: Re: [xsl] build group of
elements
> An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx,
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Datum: Sonntag, 24. Juni, 2012 20:09 Uhr
>
At 2012-06-24 17:54 +0100, henry
> human wrote:
> >Ken, unfortunatelly the
elments in the output has
> different prefixes such as:
> >
> ><_1st_
>
><_2nd_
> ><_3rd_
> >The names must contain the above prefixes
> >Your script
builds only :
> ><_1_elment
> ><_1_elment
> ><_2_elment
> ><_3_elment
> 
> Why
do you say this?  Did you not look at the results I
> added to my 
> post?  I
gave you my evidence that the correct result
> is obtained 
> with my
stylesheet ... look below again and you will see
> that what 
> you want is
produced using a conforming XSLT 2 processor (in
> my case 
> I always use
Saxon).
> 
> Maybe you get incorrect results on your machine, but it
> works
as you 
> wish on mine.  If you aren't getting what I'm getting,
> then I
suggest 
> your processor is not supporting the XSLT 2 ordinal=
> attribute
for 
> <xsl:number/>.
> 
> . . . . . . . . Ken
> 
> >--- G. Ken Holman
<gkholman@xxxxxxxxxxxxxxxxxxxx>
> schrieb am So, 24.6.2012:
> >
> > > Von: G.
Ken Holman <gkholman@xxxxxxxxxxxxxxxxxxxx>
> > > Betreff: Re: [xsl] build
group of elements
> > > An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx,
>
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > > Datum: Sonntag, 24. Juni, 2012 17:11 Uhr
> > > At 2012-06-24 15:41 +0100, henry
> > > human wrote:
> > > > sorry for my
mistake..again the input xml
> data and the
> > > expecting output:
> > > >
The xml data has many foo elements (1000).
> > >
> > > Just by conjecture, I'm
guessing you want
> <fooGroup>
> > > to be three groups of three.  You don't
>
explain the
> > > criteria, but that is the only "meaningful"
> interpretation
I
> > > can see.  Next time it would help if you
> could explain
> > > your
requirements when including an example rather
> than make
> > > us guess by
looking at your example.
> > >
> > > If I've guessed correctly, this is done
easiest
> with
> > > adjacent grouping.
> > >
> > > I hope this helps.
> > >
>
> > . . . . . . . . . Ken
> > >
> > > ~/t/ftemp $ cat henry.xml
> > > <?xml
version="1.0" encoding="UTF-8"?>
> > >  <xmldata>
> > >  <ss> s </ss>
> > > 
<nn>n </nn>
> > >   <foo>tt</foo>
> > >    <foo>bb</foo>
> > >   
<foo>aa</foo>
> > >     <foo>mm</foo>
> > >       
> <foo>ll</foo>
> > >     
 
> <foo>nn</foo>
> > >     
>    <foo>ff</foo>
> > >
> > >    <foo>gg</foo>
>
> >   
>    <foo>ii</foo>
> > >   <hh>h</hh>
> > >  </xmldata>
> > > ~/t/ftemp
$ xslt2 henry.xml henry.xsl
> > > <?xml version="1.0" encoding="UTF-8"?>
> > >
<fooGroup>
> > >    <_1st_element>
> > >
> > > <_1st_element>tt</_1st_element>
> > >
> > > <_2nd_element>bb</_2nd_element>
> > >
> > >
<_3rd_element>aa</_3rd_element>
> > >    </_1st_element>
> > >   
<_2nd_element>
> > >
> > > <_1st_element>mm</_1st_element>
> > >
> > >
<_2nd_element>ll</_2nd_element>
> > >
> > > <_3rd_element>nn</_3rd_element>
>
> >    </_2nd_element>
> > >    <_3rd_element>
> > >
> > >
<_1st_element>ff</_1st_element>
> > >
> > > <_2nd_element>gg</_2nd_element>
>
> >
> > > <_3rd_element>ii</_3rd_element>
> > >    </_3rd_element>
> > >
</fooGroup>~/t/ftemp $
> > > ~/t/ftemp $ cat henry.xsl
> > > <?xml
version="1.0"?>
> > > <xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> > >   xmlns:h="urn:X-henry"
> exclude-result-prefixes="h"
> > >   version="2.0">
> > >
> > > <xsl:output
indent="yes"/>
> > >
> > > <xsl:template match="xmldata">
> >
>   <xsl:for-each-group
> select="foo"
> > >
> > >   
>
   group-adjacent="(position()-1) idiv
> > > 9">
> > >     <fooGroup>
> > >   
>    <xsl:for-each-group
> > > select="current-group()"
> > >
> > >
> > >
group-adjacent="(position()-1) idiv 3">
> > >     
>    <xsl:element
> > >
name="_{h:ord(position())}_element">
> > >       
>    <xsl:for-each
> > >
select="current-group()">
> > >         
>    <xsl:element
> > >
name="_{h:ord(position())}_element">
> > >
> > > <xsl:copy-of
select="node()"/>
> > >
> > > </xsl:element>
> > >       
>    </xsl:for-each>
> > >     
>    </xsl:element>
> > >   
>    </xsl:for-each-group>
> > > 
   </fooGroup>
> > >   </xsl:for-each-group>
> > > </xsl:template>
> > >
> > >
<xsl:function name="h:ord">
> > >   <xsl:param name="pos"/>
> >
>   <xsl:number value="$pos"
> ordinal="1"/>
> > > </xsl:function>
> > >
> > >
</xsl:stylesheet>
> > > ~/t/ftemp $
> 
> 
> --
> Public XSLT, XSL-FO, UBL and
code list classes in Europe --
> Oct 2012
> Contact us for world-wide XML
consulting and instructor-led
> training
> Free 5-hour lecture:
http://www.CraneSoftwrights.com/links/udemy.htm
> Crane Softwrights Ltd.     
   
>   http://www.CraneSoftwrights.com/s/
> G. Ken Holman           
>    
   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Google+ profile:
https://plus.google.com/116832879756988317389/about
> Legal business
disclaimers:    http://www.CraneSoftwrights.com/legal
> 
> 
>
--~------------------------------------------------------------------
>
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> To
unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
> or e-mail:
<mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
> --~--

Current Thread