xml - XSLT: how to access previous group -
i'm struggeling on piece of code, appreciated.
to make story short, in xslt(2.0) file have 2 nested loops. first 1 performs grouping based on year value. each of these group, inner loop performs operations. in particular loop, while performing tasks in current group, need know size(the number of elements) of previous group. of course, can't figure out how achieve this.
here piece of xslt code :
<xsl:for-each-group select=" ... " group-by="year"> <xsl:for-each select="current-group()"> <!-- here want know number of elements of previous group --> </xsl:for-each> </xsl:for-each-group>
for first group, of course size needs 0 because there no previous group yet. following group (second, third, ...), need know number of elements of previous one.
so example, if have 2 groups :
- 2007 group elements data1 data2 data3 data4
- 2009 group elements data5 data6
when inner loop perform operations on '2009 group', need know 2007 group has 4 elements. (data1 data2 data3 data4)
more details !
here how xml file looks like. have root racine called 'dblp', in can find either 'proceedings' or 'inproceedings' tag. each of these consist of description of publication title,year,authors/editors, ... .
<dblp> <proceedings mdate="2007-08-28" key="conf/adma/2007"> <editor>reda alhajj</editor> <editor>hong gao</editor> <editor>xue li</editor> <editor>jianzhong li</editor> <editor>osmar r. zaïane</editor> <title>advanced data mining , applications, third international conference, adma 2007, harbin, china, august 6-8, 2007, proceedings</title> <booktitle>adma</booktitle> <series href="db/journals/lncs.html">lecture notes in computer science</series> <volume>4632</volume> <publisher>springer</publisher> <year>2009</year> <isbn>978-3-540-73870-1</isbn> <url>db/conf/adma/adma2007.html</url> </proceedings> <proceedings mdate="2007-08-28" key="conf/adma/2007"> <editor>reda alhajj</editor> <editor>hong gao</editor> <editor>xue li</editor> <editor>jianzhong li</editor> <editor>osmar r. zaïane</editor> <title>advanced data mining , applications, third international conference, adma 2007, harbin, china, august 6-8, 2007, proceedings</title> <booktitle>adma</booktitle> <series href="db/journals/lncs.html">lecture notes in computer science</series> <volume>4632</volume> <publisher>springer</publisher> <year>1999</year> <isbn>978-3-540-73870-1</isbn> <url>db/conf/adma/adma2007.html</url> </proceedings> <inproceedings mdate="2007-08-28" key="conf/adma/yanglh07"> <author>hui yang</author> <author>reda alhajj</author> <author>hongyan liu</author> <author>jun he</author> <title>delay : lazy approach mining frequent patterns on high speed data streams.</title> <pages>2-14</pages> <year>2009</year> <crossref>conf/adma/2007</crossref> <booktitle>adma</booktitle> <ee>http://dx.doi.org/10.1007/978-3-540-73871-8_2</ee> <url>db/conf/adma/adma2007.html#yanglh07</url> </inproceedings> <inproceedings mdate="2007-11-29" key="conf/adma/guokg07"> <author>yi guo</author> <author>reda alhajj</author> <author>paul wing hing kwan</author> <author>junbin gao</author> <title>learning optimal kernel distance metric in twin kernel embedding dimensionality reduction , visualization of fingerprints.</title> <pages>227-238</pages> <year>2007</year> <crossref>conf/adma/2007</crossref> <booktitle>adma</booktitle> <ee>http://dx.doi.org/10.1007/978-3-540-73871-8_22</ee> <url>db/conf/adma/adma2007.html#guokg07</url> </inproceedings>
here previous xslt piece of code :
<xsl:for-each-group select="/dblp/*/editor[text() = $currenteditor] | /dblp/*/author[text() = $currenteditor]" group-by="../year"> <xsl:value-of select="current-grouping-key()"/> <xsl:for-each select="current-group()"> <xsl:variable name="titre" select="(../title)"/> <xsl:variable name="linkee" select="(../ee)"/> </xsl:for-each> </xsl:for-each-group>
the "xsl:for-each-group" performs grouping based on year value of publications (proceedings,inproceedings). can see, select specific editor or author. exemple, $currenteditor can 'reda alhajj'. 'reda alhajj' create 3 groups : 2009 group (with 1 proceedings + 1 inproceedings), 2007 group (with 1 inproceeding) , 1999 group (with 1 proceeding) (as can see in xml file) then, inner loop, loop on element of each group retrieve more informations (title, link, ..).
my question : how can achieve same behaviour new xslt code ?
<xsl:for-each select="$groups/group"> how current-grouping-key ?? <xsl:for-each select="*"> title ?? link ??</xsl:for-each> </xsl:for-each>
big !
i reworked code provided martin hoonen, , here current xslt piece of code.
<xsl:variable name="groups"> <xsl:for-each-group select="/dblp/*/editor[text() = $currenteditor] | /dblp/*/author[text() = $currenteditor]" group-by="../year"> <group year="{../year}"> <xsl:for-each select="current-group()"> <elem titre="{../title}" linkee="{../ee}" editeurs="{../author | ../editor}"> </elem> </xsl:for-each> </group> </xsl:for-each-group> </xsl:variable> <xsl:for-each select="$groups/group"> <xsl:value-of select="@year"/> <xsl:for-each select="elem"> <xsl:variable name="person" select="@editeurs"/> <xsl:for-each select="$person"> <!-- not working loop separately on each author/editor : gives me author/editors of elem @ first iteration of loop --> <xsl:value-of select="."/> </xsl:for-each> </xsl:for-each> </xsl:for-each>
in variable "groups", have tag "group" corresponds general group (classified year). in "group" tag, have added "elem" tag corresponds publications. in deed, group can consists of several publications (i.e in 2009, there example 2 publications have 2 "elem" in "group", , each of these elem save title, ..).
my problem authors/editors. can see xml file, can have multiple distinct author or editor in publication (for example in first proceedings of xml file, have 5 editors). later able loop on each of them separately. moment, save them attribute of "elem" can see, saved string , cannot loop on each of them separately. have idea achieve ?
for moment loop on $person gives me result : hui yang reda alhajj hongyan liu jun he
instead of (separature values on wich can perfom specific treatment) :
- hui yang
- reda alhajj
- hongyan liu
- jun he
any appreciated ! :)
you need store groups variable structured needed e.g.
<xsl:variable name="groups"> <xsl:for-each-group select=" ... " group-by="year"> <group> <xsl:copy-of select="current-group()"/> </group> </xsl:for-each-group> </xsl:variable>
now here can use
<xsl:for-each select="$groups/group"> <!-- compute size of previous group --> <xsl:variable name="psize" select="count(preceding-sibling::group[1]/*)"/> <!-- iterate on elements in each group --> <xsl:for-each select="*">...</xsl:for-each> </xsl:for-each>
based on edit seems group editor
, author
elements year
of parent element , want access other elements title
or ee
of parent element. if know need these 2 values change grouping code store them e.g.
<xsl:variable name="groups"> <xsl:for-each-group select="/dblp/*/editor[. = $currenteditor] | /dblp/*/author[. = $currenteditor]" group-by="../year"> <group title="{../title}" ee="{../ee}"> <xsl:copy-of select="current-group()"/> </group> </xsl:for-each-group> </xsl:variable>
which allow access values later attributes e.g.
<xsl:for-each select="$groups/group"> <!-- compute size of previous group --> <xsl:variable name="psize" select="count(preceding-sibling::group[1]/*)"/> <!-- common, stored values --> <xsl:variable name="title" select="@title"/> <xsl:variable name="ee" select="@ee"/> <!-- iterate on elements in each group --> <xsl:for-each select="*">...</xsl:for-each> </xsl:for-each>
Comments
Post a Comment