Pregunta

Hello I have a problem with XSL

My Input is like that:

<tr class="odd">
<th scope="row" class="country" rowspan="2"><a href="">Abkhazia</a></th>
<th class="category" scope="row">Landlines</th>
<th class="locality" scope="row">All Landlines</th>
<td class="rate">11.9c</td>
</tr>
<tr class="odd">
<th class="category" scope="row">Mobiles</th>
<th class="locality" scope="row">All Networks</th>
<td class="rate">16.8c</td>
</tr>
<tr class="even">
<th scope="row" class="country" rowspan="4"><a>Algeria</a></th>
<th class="category" scope="row">Landlines</th>
<th class="locality" scope="row">All Landlines</th>
<td class="rate">5.5¢</td>
</tr>
<tr class="even">
<th class="category" scope="row" rowspan="3">Mobiles</th>
<th class="locality" scope="row">Algeria Telecom Satellite, Divona Satellite, Orascom Satellite</th>
<td class="rate">6.4¢</td>
</tr>
<tr class="even">
<th class="locality" scope="row">Mobilis</th>
<td class="rate">21.6¢</td>
</tr>
<tr class="even">
<th class="locality" scope="row">Djezzy, Nedjma, Wataniya</th>
<td class="rate">34.9¢</td>
</tr>

And I need to get Output:

 <country name="Abkhazia">
      <rate type="Landlines" operator="All Landlines" currency="EUR" vat="0" unit="minute">11.9</rate>
   </country>

   <country name="Abkhazia">
      <rate type="Mobiles" operator="All Networks" currency="EUR" vat="0" unit="minute">16.8</rate>
    </country>


<country name="Algeria">
      <rate type="Landlines" operator="All Landlines" currency="EUR" vat="0" unit="minute">5.5</rate>
   </country>
   <country name="Algeria">
      <rate type="Mobiles" operator="Algeria Telecom Satellite, Divona Satellite, Orascom Satellite" currency="EUR" vat="0" unit="minute">6.4</rate>
   </country>
   <country name="Algeria">
      <rate type="Mobiles" operator="Mobilis" currency="EUR" vat="0" unit="minute">21.6</rate>
   </country>
   <country name="Algeria">
      <rate type="Mobiles" operator="Djezzy, Nedjma, Wataniya" currency="EUR" vat="0" unit="minute">34.9</rate>
   </country>

I was using in xsl something like that:

<xsl:template match="//xhtml:tr" mode="list">

    <xsl:variable name="countryName" select="normalize-space(xhtml:th[@class = 'country'])"/>
    ...
    ...
    ...
<country>
    <xsl:attribute name="name">
        <xsl:value-of select="$countryName"/>
    </xsl:attribute>
    <rate>
    ...
    </rate>
</country>

But its not working... With that XSL I had this output:

  <country name="Abkhazia">
      <rate type="Landlines" operator="All Landlines" currency="EUR" vat="0" unit="minute">11.9</rate>    
   </country>

  <country name="Algeria">
      <rate type="Landlines" operator="All Landlines" currency="EUR" vat="0" unit="minute">5.5</rate>
   </country>
   <country name="">
      <rate type="Mobiles" operator="Algeria Telecom Satellite, Divona Satellite, Orascom Satellite" currency="EUR" vat="0" unit="minute">6.4</rate>
   </country>
   <country name="">
      <rate type="" operator="Mobilis" currency="EUR" vat="0" unit="minute">21.6</rate>
   </country>
   <country name="">
      <rate type="" operator="Djezzy, Nedjma, Wataniya" currency="EUR" vat="0" unit="minute">34.9</rate>
   </country>

Some Ideas, thanks... We can see, what I need - the country name to the second country tag.

I think I need to use some axes like parent etc. But I don't know how. Link to whole XML with problem: http://pastebin.com/jjjQeeF3 My problem start at line 156

¿Fue útil?

Solución

How about something like:

...
<xsl:for-each select="tbody/tr">
<country>
    <xsl:attribute name="name">
        <xsl:choose>
            <xsl:when test="th[@class='country']">
                <xsl:value-of select="th[@class='country']"/>       
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="preceding-sibling::tr[th/@class='country'][1]/th[@class='country']"/>    
            </xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>

    <rate>
        <!-- insert attributes -->
        <xsl:value-of select="td[@class='rate']"/>      
    </rate>
</country>
</xsl:for-each>
...

I should add that there is something not right with this format: each "country" has exactly one rate. I don't see why you need this wrapper; it would be more logical to add the country as an attribute to rate.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top