我有使用奇数和偶数类创建XSL到XML的问题

时间:2013-12-07 20:22:30

标签: xml xslt parent axes

您好XSL有问题

我的输入是这样的:

<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>

我需要输出:

 <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>

我在xsl中使用的是这样的东西:

<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>

但是它不起作用......有了这个XSL我得到了这个输出:

  <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>

一些想法,谢谢......我们可以看到,我需要的是 - 第二个国家标签的国家/地区名称。

我想我需要像父母那样使用一些轴。但我不知道如何。链接到带有问题的整个XML:http://pastebin.com/jjjQeeF3我的问题从第156行开始

1 个答案:

答案 0 :(得分:1)

如下:

...
<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>
...

我应该补充说,这种格式有些不对:每个“国家/地区”只有一种费率。我不明白为什么你需要这个包装器;将国家/地区作为费率属性更合乎逻辑。