Search code examples
xmlxsltxpathnlp

Sorting current-group() by child node element and get the result


it's me again. Sorry but this time i have a more important question for you guys. Here is the xml :

<titre>
<p type="titre">
<item><a>1</a><a>Ce</a><a>ce</a><a>PRO</a><a>PRO</a><a>n=s</a><a>0</a><a>_</a><a>0</a><a>_</a></item>
<item><a>2</a><a>que</a><a>que</a><a>PROREL</a><a>PROREL</a><a></a><a>5</a><a>obj</a><a>5</a><a>obj</a></item>
<item><a>3</a><a>l'</a><a>le</a><a>DET</a><a>DET</a><a>n=s</a><a>4</a><a>det</a><a>4</a><a>det</a></item>
<item><a>4</a><a>on</a><a>on</a><a>CLS</a><a>CLS</a><a>n=s|p=3</a><a>5</a><a>suj</a><a>5</a><a>suj</a></item>
<item><a>5</a><a>sait</a><a>savoir</a><a>V</a><a>V</a><a>n=s|t=P|p=3</a><a>1</a><a>mod_rel</a><a>1</a><a>mod_rel</a></item>
<item><a>6</a><a>de</a><a>de</a><a>P</a><a>P</a><a></a><a>5</a><a>mod</a><a>5</a><a>mod</a></item>
<item><a>7</a><a>l'</a><a>le</a><a>DET</a><a>DET</a><a>n=s</a><a>8</a><a>det</a><a>8</a><a>det</a></item>
<item><a>8</a><a>attentat</a><a>attentat</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>6</a><a>prep</a><a>6</a><a>prep</a></item>
<item><a>9</a><a>du</a><a>de</a><a>P+D</a><a>P+D</a><a>n=s|g=m</a><a>5</a><a>mod</a><a>5</a><a>mod</a></item>
<item><a>10</a><a>Nouvel</a><a>_</a><a>ADJ</a><a>ADJ</a><a></a><a>11</a><a>mod</a><a>11</a><a>mod</a></item>
<item><a>11</a><a>An</a><a>An</a><a>NPP</a><a>NPP</a><a>n=s</a><a>9</a><a>prep</a><a>9</a><a>prep</a></item>
<item><a>12</a><a>dans</a><a>dans</a><a>P</a><a>P</a><a></a><a>5</a><a>mod</a><a>5</a><a>mod</a></item>
<item><a>13</a><a>un</a><a>un</a><a>DET</a><a>DET</a><a>n=s|g=m</a><a>14</a><a>det</a><a>14</a><a>det</a></item>
<item><a>14</a><a>club</a><a>club</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>12</a><a>prep</a><a>12</a><a>prep</a></item>
<item><a>15</a><a>d'</a><a>de</a><a>P</a><a>P</a><a></a><a>5</a><a>mod</a><a>5</a><a>mod</a></item>
<item><a>16</a><a>Istanbul</a><a>_</a><a>NPP</a><a>NPP</a><a></a><a>15</a><a>prep</a><a>15</a><a>prep</a></item>
<item><a>17</a><a>.</a><a>.</a><a>PONCT</a><a>PONCT</a><a></a><a>16</a><a>ponct</a><a>16</a><a>ponct</a></item>
<item><a>18</a><a>§</a><a>§</a><a>PONCT</a><a>PONCT</a><a></a><a>16</a><a>ponct</a><a>16</a><a>ponct</a></item>
</p>
</titre>
<titre>
<p type="titre">
<item><a>1</a><a>Tiers</a><a>tiers</a><a>NC</a><a>NC</a><a>g=m</a><a>0</a><a>_</a><a>0</a><a>_</a></item>
<item><a>2</a><a>payant</a><a>payant</a><a>ADJ</a><a>ADJ</a><a>n=s|g=m</a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>3</a><a>,</a><a>,</a><a>PONCT</a><a>PONCT</a><a></a><a>2</a><a>ponct</a><a>2</a><a>ponct</a></item>
<item><a>4</a><a>paquet</a><a>paquet</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>5</a><a>neutre</a><a>neutre</a><a>ADJ</a><a>ADJ</a><a>n=s</a><a>4</a><a>mod</a><a>4</a><a>mod</a></item>
<item><a>6</a><a>,</a><a>,</a><a>PONCT</a><a>PONCT</a><a></a><a>5</a><a>ponct</a><a>5</a><a>ponct</a></item>
<item><a>7</a><a>divorce</a><a>divorce</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>8</a><a>…</a><a>…</a><a>PONCT</a><a>PONCT</a><a></a><a>7</a><a>ponct</a><a>7</a><a>ponct</a></item>
<item><a>9</a><a>ce</a><a>ce</a><a>PRO</a><a>PRO</a><a>n=s</a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>10</a><a>qui</a><a>qui</a><a>PROREL</a><a>PROREL</a><a>n=s</a><a>11</a><a>suj</a><a>11</a><a>suj</a></item>
<item><a>11</a><a>change</a><a>changer</a><a>V</a><a>V</a><a>n=s|t=P,S|p=1,3</a><a>9</a><a>mod_rel</a><a>9</a><a>mod_rel</a></item>
<item><a>12</a><a>en</a><a>en</a><a>P</a><a>P</a><a></a><a>11</a><a>mod</a><a>11</a><a>mod</a></item>
<item><a>13</a><a>janvier</a><a>janvier</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>12</a><a>prep</a><a>12</a><a>prep</a></item>
<item><a>14</a><a>2017</a><a>2017</a><a>NC</a><a>NC</a><a></a><a>13</a><a>mod</a><a>13</a><a>mod</a></item>
<item><a>15</a><a>.</a><a>.</a><a>PONCT</a><a>PONCT</a><a></a><a>14</a><a>ponct</a><a>14</a><a>ponct</a></item>
<item><a>16</a><a>§</a><a>§</a><a>PONCT</a><a>PONCT</a><a></a><a>14</a><a>ponct</a><a>14</a><a>ponct</a></item>
</p>
</titre>
<description>
<p type="description">
<item><a>1</a><a>CETA</a><a>_</a><a>NPP</a><a>NPP</a><a></a><a>9</a><a>suj</a><a>9</a><a>suj</a></item>
<item><a>2</a><a>,</a><a>,</a><a>PONCT</a><a>PONCT</a><a></a><a>1</a><a>ponct</a><a>1</a><a>ponct</a></item>
<item><a>3</a><a>TPP</a><a>_</a><a>NPP</a><a>NPP</a><a></a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>4</a><a>,</a><a>,</a><a>PONCT</a><a>PONCT</a><a></a><a>3</a><a>ponct</a><a>3</a><a>ponct</a></item>
<item><a>5</a><a>Tafta</a><a>_</a><a>NPP</a><a>NPP</a><a></a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>6</a><a>...</a><a>...</a><a>PONCT</a><a>PONCT</a><a></a><a>5</a><a>ponct</a><a>5</a><a>ponct</a></item>
<item><a>7</a><a>2016</a><a>2016</a><a>NC</a><a>NC</a><a></a><a>1</a><a>mod</a><a>1</a><a>mod</a></item>
<item><a>8</a><a>a</a><a>avoir</a><a>V</a><a>V</a><a>n=s|t=P|p=3</a><a>9</a><a>aux_tps</a><a>9</a><a>aux_tps</a></item>
<item><a>9</a><a>porté</a><a>porter</a><a>VPP</a><a>VPP</a><a>n=s|g=m|t=K</a><a>0</a><a>root</a><a>0</a><a>root</a></item>
<item><a>10</a><a>au</a><a>à</a><a>P+D</a><a>P+D</a><a>n=s|g=m</a><a>9</a><a>mod</a><a>9</a><a>mod</a></item>
<item><a>11</a><a>libre</a><a>libre</a><a>ADJ</a><a>ADJ</a><a>n=s</a><a>13</a><a>mod</a><a>13</a><a>mod</a></item>
<item><a>12</a><a>-</a><a>-</a><a>PONCT</a><a>PONCT</a><a></a><a>11</a><a>ponct</a><a>11</a><a>ponct</a></item>
<item><a>13</a><a>échange</a><a>échanger</a><a>V</a><a>V</a><a>n=s|t=P,S|p=1,3</a><a>9</a><a>mod</a><a>9</a><a>mod</a></item>
<item><a>14</a><a>un</a><a>un</a><a>DET</a><a>DET</a><a>n=s|g=m</a><a>15</a><a>det</a><a>15</a><a>det</a></item>
<item><a>15</a><a>coup</a><a>coup</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>13</a><a>obj</a><a>13</a><a>obj</a></item>
<item><a>16</a><a>dont</a><a>dont</a><a>PROREL</a><a>PROREL</a><a></a><a>18</a><a>obj</a><a>18</a><a>obj</a></item>
<item><a>17</a><a>il</a><a>il</a><a>CLS</a><a>CLS</a><a>n=s|g=m|p=3</a><a>18</a><a>suj</a><a>18</a><a>suj</a></item>
<item><a>18</a><a>pourrait</a><a>pouvoir</a><a>V</a><a>V</a><a>n=s|t=C|p=3</a><a>15</a><a>mod_rel</a><a>15</a><a>mod_rel</a></item>
<item><a>19</a><a>garder</a><a>garder</a><a>VINF</a><a>VINF</a><a>t=W</a><a>18</a><a>obj</a><a>18</a><a>obj</a></item>
<item><a>20</a><a>pour</a><a>pour</a><a>P</a><a>P</a><a></a><a>19</a><a>mod</a><a>19</a><a>mod</a></item>
<item><a>21</a><a>longtemps</a><a>longtemps</a><a>ADV</a><a>ADV</a><a></a><a>23</a><a>mod</a><a>23</a><a>mod</a></item>
<item><a>22</a><a>des</a><a>des</a><a>DET</a><a>DET</a><a>n=p</a><a>23</a><a>det</a><a>23</a><a>det</a></item>
<item><a>23</a><a>séquelles</a><a>séquelle</a><a>NC</a><a>NC</a><a>n=p|g=f</a><a>20</a><a>prep</a><a>20</a><a>prep</a></item>
<item><a>24</a><a>.</a><a>.</a><a>PONCT</a><a>PONCT</a><a></a><a>23</a><a>ponct</a><a>23</a><a>ponct</a></item>
<item><a>25</a><a>§</a><a>§</a><a>PONCT</a><a>PONCT</a><a></a><a>23</a><a>ponct</a><a>23</a><a>ponct</a></item>
</p>
</description>
<description>
<p type="description">
<item><a>1</a><a>L'</a><a>le</a><a>DET</a><a>DET</a><a>n=s</a><a>4</a><a>det</a><a>4</a><a>det</a></item>
<item><a>2</a><a>ancien</a><a>ancien</a><a>ADJ</a><a>ADJ</a><a>n=s|g=m</a><a>4</a><a>mod</a><a>4</a><a>mod</a></item>
<item><a>3</a><a>premier</a><a>premier</a><a>ADJ</a><a>ADJ</a><a>n=s|g=m</a><a>4</a><a>mod</a><a>4</a><a>mod</a></item>
<item><a>4</a><a>ministre</a><a>ministre</a><a>NC</a><a>NC</a><a>n=s|g=m</a><a>7</a><a>suj</a><a>7</a><a>suj</a></item>
<item><a>5</a><a>socialiste</a><a>socialiste</a><a>ADJ</a><a>ADJ</a><a>n=s</a><a>4</a><a>mod</a><a>4</a><a>mod</a></item>
<item><a>6</a><a>portugais</a><a>portugais</a><a>ADJ</a><a>ADJ</a><a>g=m</a><a>4</a><a>mod</a><a>4</a><a>mod</a></item>
<item><a>7</a><a>succède</a><a>succéder</a><a>V</a><a>V</a><a>n=s|t=P,S|p=1,3</a><a>0</a><a>root</a><a>0</a><a>root</a></item>
<item><a>8</a><a>à</a><a>à</a><a>P</a><a>P</a><a></a><a>7</a><a>mod</a><a>7</a><a>mod</a></item>
<item><a>9</a><a>Ban</a><a>_</a><a>NPP</a><a>NPP</a><a></a><a>8</a><a>prep</a><a>8</a><a>prep</a></item>
<item><a>10</a><a>Ki</a><a>_</a><a>NPP</a><a>NPP</a><a></a><a>9</a><a>mod</a><a>9</a><a>mod</a></item>
<item><a>11</a><a>-</a><a>-</a><a>PONCT</a><a>PONCT</a><a></a><a>10</a><a>ponct</a><a>10</a><a>ponct</a></item>
<item><a>12</a><a>moon</a><a>_</a><a>NC</a><a>NC</a><a></a><a>9</a><a>mod</a><a>9</a><a>mod</a></item>
<item><a>13</a><a>à</a><a>à</a><a>P</a><a>P</a><a></a><a>7</a><a>mod</a><a>7</a><a>mod</a></item>
<item><a>14</a><a>la</a><a>la</a><a>DET</a><a>DET</a><a>n=s|g=f</a><a>15</a><a>det</a><a>15</a><a>det</a></item>
<item><a>15</a><a>tête</a><a>tête</a><a>NC</a><a>NC</a><a>n=s|g=f</a><a>13</a><a>prep</a><a>13</a><a>prep</a></item>
<item><a>16</a><a>des</a><a>de</a><a>P+D</a><a>P+D</a><a>n=p</a><a>15</a><a>dep</a><a>15</a><a>dep</a></item>
<item><a>17</a><a>Nations</a><a>nation</a><a>NC</a><a>NC</a><a>n=p|g=f</a><a>16</a><a>prep</a><a>16</a><a>prep</a></item>
<item><a>18</a><a>unies</a><a>unir</a><a>VPP</a><a>VPP</a><a>n=p|g=f|t=K</a><a>17</a><a>mod</a><a>17</a><a>mod</a></item>
<item><a>19</a><a>.</a><a>.</a><a>PONCT</a><a>PONCT</a><a></a><a>18</a><a>ponct</a><a>18</a><a>ponct</a></item>
<item><a>20</a><a>§</a><a>§</a><a>PONCT</a><a>PONCT</a><a></a><a>18</a><a>ponct</a><a>18</a><a>ponct</a></item>
</p>
</description>

nodes items are tokens with POS, dependencies etc... I would like to keep only the nodes item where a[8] = 'obj' and i would like to sort my item by a[4], ascending. Here is my piece of code but it doesn't work. The items contains only elements with a[8]='obj' but the sort doesn't work. And i would like to concatenate a[2] and a[4] to have the word ans his POS (part of speech) How do you do that ? How to keep the nodes sorted from the template ? So my items have to be grouped by a[4] (POS), contains obj in a[8] (dependancie) and be sorted by a[4] ascending. Thx. My output must be like this :

de  DET  
garcon NC    
qui PRONREL  
marcher V  

Here is my code :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8"/>    
    
<xsl:template match="/">
    
    <xsl:apply-templates select="//titre"/>
     
</xsl:template>      
    <xsl:template match="titre">
        
        <xsl:for-each-group select="p//item[a[8]='obj']" group-by="a[4]">
            <xsl:for-each select="current-group()">
                <xsl:value-of select="concat(a[2], ' ', a[4])"/>
                <xsl:text disable-output-escaping='yes'>&#xD;&#xA;</xsl:text>
            </xsl:for-each>
        </xsl:for-each-group>
        
    </xsl:template>

    
<xsl:template match="description">
    
</xsl:template>
    
</xsl:stylesheet>

I have this output :

que PROREL  
faire VINF   
année NC   
pays NC   
l' CLO   
à P   
revenu NC  
vœux NC   
retenir VINF  
An NPP  
ans NC   
amuser VINF  
relance NC  
soutien NC  
objet NC   
identité NC  
débat NC   
être  VINF     
NC artiste  
cours NC     
hausse NC     
zone NC    

Solution

  • Both for-each and for-each-group allow you to apply sorting by using xsl:sort so if you want to order the groups use e.g.

    <xsl:for-each-group select="//p[@type='titre']//item[a[8]='obj']" group-by="a[4]">
      <xsl:sort select="current-grouping-key()"/>
    

    if you want to output the items in each group use e.g.

    <xsl:for-each select="current-group()">
      <xsl:sort select="a[2]"/>