2016-09-30 7 views
0

サーバから提供されたxmlに基づいてhtmlマークアップを生成します。要素には他の要素を含めることができます。私は、例えばXSLT 1.0xsltテンプレートは直接の子ノードには適用されません

を使用する必要があり、私はfollowinng XML構造を持っている:

<?xml version="1.0" encoding="UTF-8"?> 

<StackPanel DataContext="" HAlign="Left" Orientation="Vertical" Padding="5" VAlign="Top"> 
    <Grid Cols="2" DataContext="" HAlign="Left" Padding="5" Rows="2" VAlign="Top"> 
    <Cells> 
     <Cell Col="1" DataContext="" HAlign="Left" Padding="5" Row="0" VAlign="Top" /> 
     <Cell Col="0" DataContext="" HAlign="Left" Padding="5" Row="0" VAlign="Top"> 
      <Grid Cols="1" Rows="1"> 
       <Cells> 
        <Cell Col="0" Row="0"></Cell> 
       </Cells> 
      </Grid> 
     </Cell> 
     <Cell Col="0" DataContext="" HAlign="Left" Padding="5" Row="1" VAlign="Top" /> 
     <Cell Col="1" DataContext="" HAlign="Left" Padding="5" Row="1" VAlign="Top" /> 
     </Cells> 
    </Grid> 
</StackPanel> 

私はスタイルを適用しようとしている:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
> 
    <xsl:output method="html"/> 
    <xsl:strip-space elements="*"/> 

    <!--Entry point--> 
    <xsl:template match="/"> 
    <xsl:apply-templates/> 
    </xsl:template> 


    <!--Cell key--> 
    <xsl:key name="cell-key" match="Cells/Cell" use="@Row"/> 

    <!--Grid--> 
    <xsl:template match="Grid"> 
    <div class="grid"> 
     <xsl:apply-templates select="Cells/Cell[generate-id(.) = generate-id(key('cell-key', @Row))]" /> 
    </div> 
    </xsl:template> 

    <!--Grid cell--> 
    <xsl:template match="Cell"> 
    <div class="gridRow"> 
     <xsl:for-each select="key('cell-key', @Row)"> 
     <xsl:sort select="@Col"/> 
     <div class="gridCell"> 
      <xsl:apply-templates select="*" /> 
     </div> 
     </xsl:for-each> 
    </div> 
    </xsl:template> 
</xsl:stylesheet> 

私はグリッド行の世代のためにMuenchianメソッドを使用します。しかし、直接的な子供の代わりに私はフラットビューを得ました。

<div class="grid"> 
    <div class="gridRow"> 
    <div class="gridCell"> 
     <div class="grid"></div> 
    </div> 
    <div class="gridCell"></div> 
    <div class="gridCell"></div> 
    </div> 
    <div class="gridRow"> 
    <div class="gridCell"></div> 
    <div class="gridCell"></div> 
    </div> 
</div> 

セルをネストされたグリッドに配置するにはどうすればよいですか?

+0

はあなたが、この場合に予想される出力を表示するためにあなたの質問を編集することはできますか?ありがとう! –

答えて

1

「失敗」(良好難しい状況である)は、xsl:key介して外側グリッドの同じ@Row<Cell Col="0" Row="0"></Cell> [内側グリッドの一部]を一般化。行番号は同じですが、異なるグリッド深度です。

あなたはそれらを分離して独立させる必要があります。

一つのアプローチ:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    > 
    <xsl:output method="html"/> 
    <xsl:strip-space elements="*"/> 

    <!--Entry point--> 
    <xsl:template match="/"> 
     <xsl:apply-templates/> 
    </xsl:template> 


    <!--Cell key--> 
    <xsl:key name="cell-key" match="Cells/Cell" use="concat(count(ancestor::Grid), '|', @Row)"/> 

    <!--Grid--> 
    <xsl:template match="Grid"> 
     <div class="grid"> 
      <xsl:apply-templates select="Cells/Cell[generate-id(.) = generate-id(key('cell-key', concat(count(ancestor::Grid), '|', @Row)))]" /> 
     </div> 
    </xsl:template> 

    <!--Grid cell--> 
    <xsl:template match="Cell"> 
     <div class="gridRow"> 
      <xsl:for-each select="key('cell-key', concat(count(ancestor::Grid), '|', @Row))"> 
       <xsl:sort select="@Col"/> 
       <div class="gridCell"> 
        <xsl:apply-templates select="*" /> 
       </div> 
      </xsl:for-each> 
     </div> 
    </xsl:template> 
</xsl:stylesheet> 

結果

<div class="grid"> 
    <div class="gridRow"> 
    <div class="gridCell"> 
     <div class="grid"> 
      <div class="gridRow"> 
       <div class="gridCell"></div> 
      </div> 
     </div> 
    </div> 
    <div class="gridCell"></div> 
    </div> 
    <div class="gridRow"> 
    <div class="gridCell"></div> 
    <div class="gridCell"></div> 
    </div> 
</div> 
関連する問題