2

グループ化された結果を3列で出力したい。私はテーブルを使用していないので、結果の3つのセクション(HTMLを参照)を並べて意味していると思います。

いずれかのノードの値に従って結果をグループ化し、ノードの値である見出しの下にグループ化しました。例えば:

<All_Results>
  <Result>
   <Dept>Finance</Dept>
   <Name>Bob</Name>
  </Result>
  <Result>
   <Dept>Finance</Dept>
   <Name>Susan</Name>
  </Result>
  <Result>
   <Dept>Sales</Dept>
   <Name>Igor</Name>
  </Result>
</All_Results>

次のようにフォーマットされます:

 <li>
    <h4>Finance</h4>
    <ul>
      <li>Bob</li>
    </ul>
    <ul>
      <li>Susan</li>
    </ul>

    <h4>Sales</h4>
    <ul>
      <li>Igor</li>
    </ul>
 </li>

そしてこれはうまくいき、私はそれに満足しています。今私がやろうとしているのは、部門とその結果(つまり、この例では名前)を並べて3つの列を作成することです。私は9つの可能な部門を期待していますが、これは将来変更される可能性があります。

これが私のXSLTです(これまでのところ、上記のフォーマットだけで、列に対する作業はまだありません。この問題へのアプローチ方法がわかりません):

<xsl:output method="html" />
<xsl:key name="results-by-dept" match="Result" use="Dept" />
<xsl:template match="All_Results">
  <xsl:variable name="Rows" select="Result" />
  <xsl:variable name="RowCount" select="count($Rows)" />
  <ul class="deptList">
    <xsl:for-each select="Result[count(. | key('results-by-dept', Dept)[1]) = 1]">
      <xsl:sort select="Dept"  order ="ascending"/>
      <li>
        <h4>
          <xsl:value-of select="Dept" />
        </h4>
        <ul>
          <xsl:for-each select="key('results-by-dept', Dept)">
            <xsl:sort select="Name" />
            <li>
                <xsl:value-of select="Name"/>
            </li>
          </xsl:for-each>
        </ul>
      </li>
    </xsl:for-each>
  </ul>
</xsl:template>
</xsl:stylesheet>

どんな助けでも大歓迎です!


ショーンの編集

OPと話し合った後、このユースケースは彼が何を望んでいるかを示していると思います。次のサンプル入力ドキュメントは、1人または2人の従業員がいる5つの部門を示しています。ノードの順序は重要ではありません。

ユースケース1:入力ドキュメント

<All_Results>
    <Result>
        <Dept>Finance</Dept>
        <Name>Bob</Name>
    </Result>
    <Result>
        <Dept>Engineering</Dept>
        <Name>Inna</Name>
    </Result>
    <Result>
        <Dept>Finance</Dept>
        <Name>Susan</Name>
    </Result>
    <Result>
        <Dept>Sales</Dept>
        <Name>Igor</Name>
    </Result>
    <Result>
        <Dept>Human resources</Dept>
        <Name>Jane</Name>
    </Result>
    <Result>
        <Dept>Admin</Dept>
        <Name>Joe</Name>
    </Result>
    <Result>
        <Dept>Engineering</Dept>
        <Name>Dima</Name>
    </Result>
    <Result>
        <Dept>Human resources</Dept>
        <Name>Beth</Name>
    </Result>
</All_Results>

出力はそのように変換されます(以下のリスト)。HTMLテーブルは、3列で構成されますが、1行のみで構成されます。各セルには、部門名を先頭にした部門従業員のHTML順不同リストが含まれます。各列には、部門の約3分の1が含まれ、最後の列はこの点で不規則になっています。トップダウン、次に左から右に読むと、部門はアルファベット順にソートする必要があります。各部門内で、従業員をアルファベット順にリストする必要があります。

ユースケース1:出力ドキュメント

<table>
    <tr>
        <td>
            <ul class="deptList">
                <li>
                    <h4>Admin</h4>
                    <ul>
                        <li>Joe</li>
                    </ul>
                </li>
                <li>
                    <h4>Engineering</h4>
                    <ul>
                        <li>Dima</li>
                        <li>Inna</li>
                    </ul>
                </li>
            </ul>
        </td>
        <td>
            <ul class="deptList">
                <li>
                    <h4>Finance</h4>
                    <ul>
                        <li>Bob</li>
                        <li>Susan</li>
                    </ul>
                </li>
                <li>
                    <h4>Human resources</h4>
                    <ul>
                        <li>Beth</li>
                        <li>Jane</li>
                    </ul>
                </li>
            </ul>
        </td>
        <td>
            <ul class="deptList">
                <li>
                    <h4>Sales</h4>
                    <ul>
                        <li>Igor</li>
                    </ul>
                </li>
            </ul>
        </td>
    </tr>
</table>
4

3 に答える 3

2

この XSLT 1.0 変換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kDeptByVal" match="Dept" use="."/>
 <xsl:key name="kEmpByDept" match="Name" use="../Dept"/>

 <xsl:variable name="vDeptsUniq" select=
  "/*/*/Dept[generate-id() = generate-id(key('kDeptByVal',.)[1])]"/>

 <xsl:variable name="vDeptsPerCol" select="ceiling(count($vDeptsUniq) div 3)"/>

 <xsl:template match="/*">
  <table border="1">
    <tr>
      <xsl:for-each select="$vDeptsUniq[not(position() > 3)]">
        <xsl:variable name="vCol" select="position()"/>
        <td>
          <ul class="deptList">
           <xsl:for-each select="$vDeptsUniq">
            <xsl:sort/>
            <xsl:if test=
             "position() > ($vCol -1)*$vDeptsPerCol
             and
              not(position() > $vCol*$vDeptsPerCol)
             ">
                <li>
                      <h4><xsl:value-of select="."/></h4>
                    <ul>
                      <xsl:apply-templates select="key('kEmpByDept', .)">
                       <xsl:sort/>
                      </xsl:apply-templates>
                    </ul>
                </li>
            </xsl:if>
           </xsl:for-each>
          </ul>
        </td>
      </xsl:for-each>
    </tr>
  </table>
 </xsl:template>

 <xsl:template match="Name">
  <li><xsl:value-of select="."/></li>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<All_Results>
    <Result>
        <Dept>Finance</Dept>
        <Name>Bob</Name>
    </Result>
    <Result>
        <Dept>Engineering</Dept>
        <Name>Inna</Name>
    </Result>
    <Result>
        <Dept>Finance</Dept>
        <Name>Susan</Name>
    </Result>
    <Result>
        <Dept>Sales</Dept>
        <Name>Igor</Name>
    </Result>
    <Result>
        <Dept>Human resources</Dept>
        <Name>Jane</Name>
    </Result>
    <Result>
        <Dept>Admin</Dept>
        <Name>Joe</Name>
    </Result>
    <Result>
        <Dept>Engineering</Dept>
        <Name>Dima</Name>
    </Result>
    <Result>
        <Dept>Human resources</Dept>
        <Name>Beth</Name>
    </Result>
</All_Results>

必要な正しい結果が生成されます

<table border="1">
   <tr>
      <td>
         <ul class="deptList">
            <li>
               <h4>Admin</h4>
               <ul>
                  <li>Joe</li>
               </ul>
            </li>
            <li>
               <h4>Engineering</h4>
               <ul>
                  <li>Dima</li>
                  <li>Inna</li>
               </ul>
            </li>
         </ul>
      </td>
      <td>
         <ul class="deptList">
            <li>
               <h4>Finance</h4>
               <ul>
                  <li>Bob</li>
                  <li>Susan</li>
               </ul>
            </li>
            <li>
               <h4>Human resources</h4>
               <ul>
                  <li>Beth</li>
                  <li>Jane</li>
               </ul>
            </li>
         </ul>
      </td>
      <td>
         <ul class="deptList">
            <li>
               <h4>Sales</h4>
               <ul>
                  <li>Igor</li>
               </ul>
            </li>
         </ul>
      </td>
   </tr>
</table>
于 2012-08-01T04:05:17.233 に答える
1

XSLT 1.0 でのアプローチの 1 つを次に示します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:key name="depts" match="Dept" use="." />
  <xsl:key name="results-by-dept" match="Result" use="Dept" />

  <xsl:template match="All_Results">
    <xsl:variable name="max-cols" select="3"/>
    <xsl:variable name="depts"
                  select="Result/Dept[count(.|key('depts', .)[1]) = 1]"/>
    <xsl:variable name="col-size"
                  select="ceiling(count($depts) div $max-cols)"/>
    <table>
      <tr>
        <xsl:for-each select="$depts[(position() - 1) mod $col-size = 0]">
          <xsl:variable name="col" select="position() - 1"/>
          <td>
            <ul class="deptList">
              <xsl:for-each select="$depts">
                <xsl:sort select="."/>
                <xsl:if test="floor((position() - 1) div $col-size) = $col">
                  <li>
                    <xsl:apply-templates select="."/>
                  </li>
                </xsl:if>
              </xsl:for-each>
            </ul>
          </td>
        </xsl:for-each>
      </tr>
    </table>
  </xsl:template>


  <xsl:template match="Dept">
    <h4>
      <xsl:value-of select="." />
    </h4>
    <ul>
      <xsl:for-each select="key('results-by-dept', .)">
        <xsl:sort select="Name" />
        <li>
          <xsl:value-of select="Name"/>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>

</xsl:stylesheet>

いくつかのメモ:

  • ここでの主なトリックは、$depts[(position() - 1) mod $col-size = 0]列の数だけループを使用することです。
  • 列をループする別の方法は、再帰テンプレートを使用してカウンター変数をインクリメントすることです。
  • これは、XSLT 2.0 ではより簡単になります。
  • Muenchian グループ化count(.|key('depts', .)[1]) = 1述語は、同等に次のように記述できます。あいまいさが少ないと思うので後者を好みますが、質問で使用されたものであるため、ここでは前者を使用しました。generate-id() = generate-id(key('depts', .)[1])
于 2012-07-31T20:25:52.267 に答える
1

この XSLT 1.0 スタイルシートは、3 つの列を提供します ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="kDepartments" match="Result" use="Dept" />

<xsl:template match="/">
 <table>
  <tr>
   <xsl:variable name="dept-count" select="count( */Result[
           generate-id(.) = generate-id( key('kDepartments',Dept)[1])])" />
   <xsl:for-each select="(//*)[position() &lt;= 3]">
     <xsl:variable name="column" select="position()" />
     <td>
       <ul class="deptList">
         <xsl:for-each select="/*/Result[
           generate-id(.) = generate-id( key('kDepartments',Dept)[1])]" >
            <xsl:sort select="Dept" />
            <xsl:variable name="DeptIndex" select="position()" /> 
            <xsl:apply-templates select="self::Result[
              (floor((($DeptIndex - 1)*3 div $dept-count)) + 1) = $column]" /> 
         </xsl:for-each>    
       </ul>  
     </td>  
   </xsl:for-each>  
  </tr>
 </table>
</xsl:template>

<xsl:template match="Result">
 <h4>
  <xsl:value-of select="Dept" />
 </h4>
    <ul>
      <xsl:for-each select="key('kDepartments', Dept)">
        <xsl:sort select="Name" />
        <li>
          <xsl:value-of select="Name"/>
        </li>
      </xsl:for-each>
    </ul>
</xsl:template>

</xsl:stylesheet>
于 2012-08-01T01:08:42.680 に答える