这是我的XML:
<SECTION_CONTENT_LIST>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<REGION>10-Mediterraneo Occidentale - Francia</REGION>
<ITA_LIGHT_NUMBER>0840</ITA_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<REGION>10-Mediterraneo Occidentale - Francia</REGION>
<ITA_LIGHT_NUMBER>0843</ITA_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<REGION>10-Mediterraneo Occidentale - Francia</REGION>
<ITA_LIGHT_NUMBER>0850</ITA_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<REGION>16-Mar Tirreno - Francia (Corsica)</REGION>
<ITA_LIGHT_NUMBER>0906</ITA_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<REGION>16-Mar Tirreno - Francia (Corsica)</REGION>
<ITA_LIGHT_NUMBER>0922.15</ITA_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
<SECTION_CONTENT_LIST_ITEM>
<NTC_LIGHTLISTPRODUCT>
<REGION>25-Mare Adriatico - Slovenia</REGION>
<ITA_LIGHT_NUMBER>3620</ITA_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>
</SECTION_CONTENT_LIST_ITEM>
</SECTION_CONTENT_LIST>
这是我的XSLT 1.0:
<table style='border-collapse:collapse; width:100%; align:center' cellspacing="0" cellpadding="0">
<xsl:for-each select="//ITA_LIGHT_NUMBER">
<xsl:sort select="." order="ascending" data-type="text"/>
<tr style="line-height:0.78cm">
<xsl:variable name="regione" select="preceding-sibling::REGION"/>
<td style="height:0.68cm;border-bottom:dotted 1.0">
<xsl:if test="following::REGION != $regione">
<xsl:value-of select="preceding-sibling::REGION"/>
</xsl:if>
</td>
<td style="height:0.68cm;border-bottom:dotted 1.0">
<xsl:value-of select="."/>
</td>
</tr>
</xsl:for-each>
</table>
这将在两列中创建一个表。在第一列中写入区域,在第二列中写入ITA_LIGHT_NUMBER。
10-Mediterraneo Occidentale - Francia 0840
10-Mediterraneo Occidentale - Francia 0843
10-Mediterraneo Occidentale - Francia 0850
16-Mar Tirreno - Francia(科西嘉)0906
16-Mar Tirreno - Francia(科西嘉岛)0922.15
25-Mare Adriatico - 斯洛文尼亚3620
但我想要这个输出:
10-Mediterraneo Occidentale - Francia 0840 - 0850
16-Mar Tirreno - Francia(科西嘉)0906 - 0922.15
25-Mare Adriatico - 斯洛文尼亚3620
实际上:写下第一个区域和第一个ITA_LIGHT_NAME,如果以下REGION与当前写入ITA_LIGHT_NUMBER不同。
答案 0 :(得分:1)
如果使用密钥,情况会好得多。
例如在XSLT的开头定义它:
<xsl:key name="lightproducts" match="NTC_LIGHTLISTPRODUCT" use="REGION"/>
您可以按如下方式修改模板:
<table style='border-collapse:collapse; width:100%; align:center' cellspacing="0" cellpadding="0">
<xsl:for-each select="//NTC_LIGHTLISTPRODUCT">
<xsl:sort select="REGION" order="ascending" data-type="text"/>
<xsl:variable name="regione" select="REGION" />
<xsl:if test="not(preceding::NTC_LIGHTLISTPRODUCT[REGION/text() = $regione])">
<tr style="line-height:0.78cm">
<td style="height:0.68cm;border-bottom:dotted 1.0">
<xsl:value-of select="$regione"/>
</td>
<td style="height:0.68cm;border-bottom:dotted 1.0">
<xsl:for-each select="key('lightproducts', $regione)">
<xsl:sort select="ITA_LIGHT_NUMBER"/>
<xsl:choose>
<xsl:when test="last() = 1">
<xsl:value-of select="ITA_LIGHT_NUMBER"/>
</xsl:when>
<xsl:when test="position() = 1">
<xsl:value-of select="ITA_LIGHT_NUMBER"/>
</xsl:when>
<xsl:when test="position() = last()">
<xsl:text>-</xsl:text>
<xsl:value-of select="ITA_LIGHT_NUMBER"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
这是我通过您的输入获得的
<?xml version="1.0" encoding="utf-8"?>
<table style="border-collapse:collapse; width:100%; align:center" cellspacing="0" cellpadding="0">
<tr style="line-height:0.78cm">
<td style="height:0.68cm;border-bottom:dotted 1.0">10-Mediterraneo Occidentale - Francia</td>
<td style="height:0.68cm;border-bottom:dotted 1.0">0840-0850</td>
</tr>
<tr style="line-height:0.78cm">
<td style="height:0.68cm;border-bottom:dotted 1.0">16-Mar Tirreno - Francia (Corsica)</td>
<td style="height:0.68cm;border-bottom:dotted 1.0">0906-0922.15</td>
</tr>
<tr style="line-height:0.78cm">
<td style="height:0.68cm;border-bottom:dotted 1.0">25-Mare Adriatico - Slovenia</td>
<td style="height:0.68cm;border-bottom:dotted 1.0">3620</td>
</tr>
</table>