1

我一直在阅读 Muenchian 方法,但我只是不了解如何生成我需要的 html 格式的分组。

这是源xml。

<?xml version="1.0" encoding="UTF-8"?>
<env>
<paramCount>4</paramCount>
<param parameter="id" value="1371"/>
<param parameter="xform" value="none"/>
<param parameter="user" value="Administrator"/>
<param parameter="time-stamp" value="Jun 5, 2012 2:29:45 PM"/>
<sql name="Matrix Demo" qid="1371">
    <row num="1">
        <col meta="MATRIX.X_TITLE" grp="0" label="X" type="12" name="X">1-Sample Topic</col>
        <col meta="MATRIX.X_ID" grp="0" label="X_ID" type="2" name="X_ID">3838</col>
        <col meta="MATRIX.Y_TITLE" grp="0" label="Y" type="12" name="Y">DC</col>
        <col meta="MATRIX.Y_ID" grp="0" label="Y_ID" type="2" name="Y_ID">4909</col>
        <col meta="MATRIX.PRIORITY" grp="0" label="Priority" type="2" name="Priority">1740</col>
    </row>
    <row num="2">
        <col meta="MATRIX.X_TITLE" grp="0" label="X" type="12" name="X">1-Sample Topic</col>
        <col meta="MATRIX.X_ID" grp="0" label="X_ID" type="2" name="X_ID">3838</col>
        <col meta="MATRIX.Y_TITLE" grp="0" label="Y" type="12" name="Y">Maryland</col>
        <col meta="MATRIX.Y_ID" grp="0" label="Y_ID" type="2" name="Y_ID">4910</col>
        <col meta="MATRIX.PRIORITY" grp="0" label="Priority" type="2" name="Priority">1740</col>
    </row>
    <row num="3">
        <col meta="MATRIX.X_TITLE" grp="0" label="X" type="12" name="X">1-Sample Topic</col>
        <col meta="MATRIX.X_ID" grp="0" label="X_ID" type="2" name="X_ID">3838</col>
        <col meta="MATRIX.Y_TITLE" grp="0" label="Y" type="12" name="Y">Virginia</col>
        <col meta="MATRIX.Y_ID" grp="0" label="Y_ID" type="2" name="Y_ID">4908</col>
        <col meta="MATRIX.PRIORITY" grp="0" label="Priority" type="2" name="Priority">1740</col>
    </row>
    <row num="4">
        <col meta="MATRIX.X_TITLE" grp="0" label="X" type="12" name="X">2-Sample Topic</col>
        <col meta="MATRIX.X_ID" grp="0" label="X_ID" type="2" name="X_ID">4901</col>
        <col meta="MATRIX.Y_TITLE" grp="0" label="Y" type="12" name="Y">DC</col>
        <col meta="MATRIX.Y_ID" grp="0" label="Y_ID" type="2" name="Y_ID">4909</col>
        <col meta="MATRIX.PRIORITY" grp="0" label="Priority" type="2" name="Priority">1741</col>
    </row>
    <row num="5">
        <col meta="MATRIX.X_TITLE" grp="0" label="X" type="12" name="X">2-Sample Topic</col>
        <col meta="MATRIX.X_ID" grp="0" label="X_ID" type="2" name="X_ID">4901</col>
        <col meta="MATRIX.Y_TITLE" grp="0" label="Y" type="12" name="Y">Maryland</col>
        <col meta="MATRIX.Y_ID" grp="0" label="Y_ID" type="2" name="Y_ID">4910</col>
        <col meta="MATRIX.PRIORITY" grp="0" label="Priority" type="2" name="Priority">1741</col>
    </row>
    <row num="6">
        <col meta="MATRIX.X_TITLE" grp="0" label="X" type="12" name="X">2-Sample Topic</col>
        <col meta="MATRIX.X_ID" grp="0" label="X_ID" type="2" name="X_ID">4901</col>
        <col meta="MATRIX.Y_TITLE" grp="0" label="Y" type="12" name="Y">Virginia</col>
        <col meta="MATRIX.Y_ID" grp="0" label="Y_ID" type="2" name="Y_ID">4908</col>
        <col meta="MATRIX.PRIORITY" grp="0" label="Priority" type="2" name="Priority">1741</col>
    </row>
    ...
</sql>
</env>

这就是我想要达到的目标。

<html>
<body>
<table border="1" width="500px">
<thead>
    <tr>
        <th>&#160;</th>
        <th>1-Sample Topic</th>
        <th>2-Sample Topic</th>
        <th>3-Sample Topic</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>DC</td>
        <td>1740</td>
        <td>1741</td>
        <td>1742</td>
    </tr>
    <tr>
        <td>Maryland</td>
        <td>1740</td>
        <td>1741</td>
        <td>1742</td>
    </tr>
    <tr>
        <td>Virginia</td>
        <td>1740</td>
        <td>1741</td>
        <td>1742</td>
    </tr>
</tbody>
</table>
</body>
</html>

我还没有真正接近获得我需要的输出。我确实从http://www.jenitennison.com/xslt/grouping/muenchian.html得到了这个例子,但没有比这更进一步。

我可以控制 col 部分中输出的数据,但我无法更改整体树。任何帮助将不胜感激。

4

1 回答 1

1

您没有说哪些<col>元素是最可靠的分组元素,但我认为具有name="X_ID"和的name="Y_ID"元素是合适的。

Muenchian 方法的目的是快速定位具有相同特征的所有元素。所以在这种情况下,我们需要根据行的X_ID列值对行进行分组,以在第一行的元素中找到要进入的文本<th>,并分别按Y_ID列的值来确定在<td>后续行的元素中显示哪些行.

此样式表按您指定的方式进行分组。将其应用于您提供的数据时的输出显示在其下方。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="html" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="rows_by_x_id" match="row" use="col[@name='X_ID']" />
  <xsl:key name="rows_by_y_id" match="row" use="col[@name='Y_ID']" />

  <xsl:template match="/env/sql">

    <html>
      <body>

        <table border="1" width="500px">

          <tr>
            <th>&#160;</th>
            <xsl:for-each select="row[generate-id() = generate-id(key('rows_by_x_id', col[@name='X_ID']))]">
              <th>
                <xsl:value-of select="col[@name='X']"/>
              </th>
            </xsl:for-each>
          </tr>

          <xsl:for-each select="row[generate-id() = generate-id(key('rows_by_y_id', col[@name='Y_ID']))]">
            <tr>
              <td>
                <xsl:value-of select="col[@name='Y']"/>
              </td>
              <xsl:for-each select="key('rows_by_y_id', current()/col[@name='Y_ID'])">
                <td>
                  <xsl:value-of select="col[@name='Priority']"/>
                </td>
              </xsl:for-each>
            </tr>
          </xsl:for-each>

        </table>

      </body>
    </html>

  </xsl:template>

</xsl:stylesheet>

输出

4<html>
   <body>
      <table border="1" width="500px">
         <tr>
            <th>&nbsp;</th>
            <th>1-Sample Topic</th>
            <th>2-Sample Topic</th>
         </tr>
         <tr>
            <td>DC</td>
            <td>1740</td>
            <td>1741</td>
         </tr>
         <tr>
            <td>Maryland</td>
            <td>1740</td>
            <td>1741</td>
         </tr>
         <tr>
            <td>Virginia</td>
            <td>1740</td>
            <td>1741</td>
         </tr>
      </table>
   </body>
</html>
于 2012-06-05T20:32:53.397 回答