【问题标题】:Create page index -Xslt 1.0 -创建页面索引 -Xslt 1.0 -
【发布时间】:2015-03-18 15:27:59
【问题描述】:

这是我的 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>

这会在两列中创建一个表。在第一列写 Region,在第二列写 ITA_LIGHT_NUMBER。

10-Mediterraneo Occidentale - 法国 0840
10-Mediterraneo Occidentale - 法国 0843
10-Mediterraneo Occidentale - 法国 0850
3 月 16 日蒂雷诺 - 弗朗西亚(科西嘉岛)0906
3 月 16 日蒂雷诺 - 弗朗西亚(科西嘉岛)0922.15
25-Mare Adriatico - 斯洛文尼亚 3620

但我想要这个输出:

10-Mediterraneo Occidentale - 法国 0840 - 0850
3 月 16 日蒂雷诺 - 弗朗西亚 (科西嘉岛) 0906 - 0922.15
25-Mare Adriatico - 斯洛文尼亚 3620

实际:写入第一个区域和第一个 ITA_LIGHT_NAME,如果后面的 REGION 与当前不同,则写入 ITA_LIGHT_NUMBER。

【问题讨论】:

  • 这是一个分组问题。要了解如何在 XSLT 1.0 中执行此操作,请参阅:jenitennison.com/xslt/grouping/muenchian.html 然后搜索标记为 XSLT 1.0grouping 的问题以找到大量示例。
  • 你能帮我 michael.hor257k。谢谢。我可以在不使用密钥的情况下执行此操作吗?
  • 它可以在不使用密钥的情况下完成,但基于密钥的方法运行起来可能会更加高效。为什么限制,出于兴趣?我用过的每个处理器都支持密钥。

标签: xslt xslt-1.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>

【讨论】:

  • @Frik 如果此答案解决了您的问题,请接受。谢谢!
  • 谢谢potame's 我明天试试......但我认为它有效。谢谢。
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多