【问题标题】:"Transforming xml into html table with too many unknowns" Follow-Up《把xml转成html表,未知数太多》后续
【发布时间】:2013-01-26 10:46:58
【问题描述】:

这是帖子“Transforming xml into html table with too many unknowns”的第 2 部分 除了为每种语言创建一个包含不同行的 html 表之外,我还需要为每种语言创建一个不同的行,其中包含来自 descripGrp 的信息。这是原始 XML 文件的摘录,其中包含语言“CS”的 1 个 descripGrp

<mtf>
  <conceptGrp>
    <concept>1</concept>

    <languageGrp>
      <language lang="DE" type="Deutsch"/>

      <termGrp>
        <term>Abbildung</term>
      </termGrp>
    </languageGrp>

    <languageGrp>
      <language lang="PL" type="Polnisch"/>

      <termGrp>
        <term>ilustracja</term>
      </termGrp>
    </languageGrp>

    <languageGrp>
      <language lang="RU" type="Russisch"/>

      <termGrp>
        <term>иллюстрация</term>
      </termGrp>

      <termGrp>
        <term>рисунок</term>
      </termGrp>
    </languageGrp>

    <languageGrp>
      <language lang="CS" type="Tschechisch"/>

      <termGrp>
        <term>vyobrazení</term>

        <descripGrp>
          <descrip type="Autor">MK</descrip>
        </descripGrp>
      </termGrp>
    </languageGrp>
  </conceptGrp>
</mtf>

“descripGrp”可以有各种“类型”,并且可能出现在每个“languageGrp/termGrP”中,或者可能一起丢失。感谢 JLRishe 为第一个问题提供了出色的答案,第一个问题是答案,但是这里提到的添加仍然是开放的。感谢您的输入。非常非常感谢。

【问题讨论】:

    标签: html xml xslt html-table


    【解决方案1】:

    这是我想出的:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" encoding="utf-8" indent="yes"/>
      <xsl:key name="kLang" match="languageGrp" use="language/@lang" />
      <xsl:key name="kDescrip" match="descrip"
               use="concat(../../../language/@lang, '+', @type)" />
      <xsl:variable name="uniqueLangs"
                    select="/mtf/conceptGrp/languageGrp
                      [generate-id() =  generate-id(key('kLang', language/@lang)[1])]" />
      <!-- A set of the first item of each <descrip>, grouped by @lang and @type -->
      <xsl:variable name="uniqueDescrip"
                    select="/mtf/conceptGrp/languageGrp/termGrp/descripGrp/descrip
                      [generate-id() = 
                         generate-id(key('kDescrip', 
                            concat(../../../language/@lang, '+', @type))[1])]" />
    
      <xsl:template match="mtf">
        <html>
          <body>
            <h2>Terminologie</h2>
            <table>
              <tr>
                <!--Zeile 1-->
                <td>Konzept-ID</td>
                <xsl:apply-templates select="$uniqueLangs" mode="header"/>
              </tr>
              <xsl:apply-templates/>
            </table>
          </body>
        </html>
      </xsl:template>
    
    
      <xsl:template match="conceptGrp">
        <xsl:apply-templates select="languageGrp" mode="maxGroup">
          <xsl:sort select="count(termGrp)" order="descending" data-type="number"/>
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="languageGrp" mode="header">
        <td>
          <xsl:value-of select="language/@type"/>
        </td>
        <xsl:apply-templates
              select="$uniqueDescrip[../../../language/@lang = 
                                 current()/language/@lang]"
              mode="header" />
      </xsl:template>
    
      <xsl:template match="descrip" mode="header">
        <td>
          <xsl:value-of select="@type"/>
        </td>
      </xsl:template>
    
      <xsl:template match="languageGrp" mode="maxGroup">
        <!-- Only use the first group out of the sorting (most termGrps) -->
        <xsl:if test="position() = 1">
          <xsl:apply-templates select="termGrp" />
        </xsl:if>
      </xsl:template>
    
      <xsl:template match="termGrp">
        <tr>
          <td>
            <xsl:value-of select="../../concept"/>
          </td>
          <!-- Iterate through all of the distinct languages -->
          <xsl:apply-templates select="$uniqueLangs" mode="item">
            <xsl:with-param name="currConcept" select="../.." />
            <xsl:with-param name="pos" select="position()" />
          </xsl:apply-templates>
        </tr>
      </xsl:template>
    
      <xsl:template match="languageGrp" mode="item">
        <xsl:param name="currConcept" />
        <xsl:param name="pos" />
    
        <xsl:variable name="matchingTermGrp"
                      select="$currConcept/languageGrp[language/@lang = 
                                                       current()/language/@lang]
                                                       /termGrp[$pos]" />
        <td>
          <xsl:value-of select="$matchingTermGrp/term"/>
        </td>
        <xsl:apply-templates
          select="$uniqueDescrip[../../../language/@lang = current()/language/@lang]"
          mode="value">
          <xsl:with-param name="termGrp" select="$matchingTermGrp" />
        </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="descrip" mode="value">
        <xsl:param name="termGrp" />
        <td>
          <xsl:value-of select="$termGrp/descripGrp/descrip[@type = current()/@type]" />
        </td>
      </xsl:template>
    </xsl:stylesheet>
    

    在此输入 XML 上运行时:

    <mtf>
      <conceptGrp>
        <concept>1</concept>
    
        <languageGrp>
          <language lang="DE" type="Deutsch"/>
    
          <termGrp>
            <term>Abbildung</term>
          </termGrp>
        </languageGrp>
    
        <languageGrp>
          <language lang="PL" type="Polnisch"/>
    
          <termGrp>
            <term>ilustracja</term>
          </termGrp>
        </languageGrp>
    
        <languageGrp>
          <language lang="RU" type="Russisch"/>
    
          <termGrp>
            <term>иллюстрация</term>
            <descripGrp>
              <descrip type="Other">Sales</descrip>
            </descripGrp>
          </termGrp>
    
          <termGrp>
            <term>рисунок</term>
            <descripGrp>
              <descrip type="Other">Marketing</descrip>
            </descripGrp>
          </termGrp>
        </languageGrp>
    
        <languageGrp>
          <language lang="CS" type="Tschechisch"/>
    
          <termGrp>
            <term>vyobrazení</term>
    
            <descripGrp>
              <descrip type="Autor">MK</descrip>
              <descrip type="Kontext">Marketing</descrip>
            </descripGrp>
          </termGrp>
        </languageGrp>
      </conceptGrp>
    </mtf>
    

    这会产生:

    <html>
      <body>
        <h2>Terminologie</h2>
        <table>
          <tr>
            <td>Konzept-ID</td>
            <td>Deutsch</td>
            <td>Polnisch</td>
            <td>Russisch</td>
            <td>Other</td>
            <td>Tschechisch</td>
            <td>Autor</td>
            <td>Kontext</td>
          </tr>
          <tr>
            <td>1</td>
            <td>Abbildung</td>
            <td>ilustracja</td>
            <td>иллюстрация</td>
            <td>Sales</td>
            <td>vyobrazení</td>
            <td>MK</td>
            <td>Marketing</td>
          </tr>
          <tr>
            <td>1</td>
            <td></td>
            <td></td>
            <td>рисунок</td>
            <td>Marketing</td>
            <td></td>
            <td></td>
            <td></td>
          </tr>
        </table>
      </body>
    </html>
    

    【讨论】:

    • 我最深切和无尽的感激。它完美地工作!这就是我需要的。非常感谢。我会怎么做....
    • 哈哈,乐于助人。请记住通过单击旁边的复选标记图标将答案标记为正确。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多