2016-09-30 1 views
0

서버에서 제공하는 XML을 기반으로 HTML 마크 업을 생성하고 싶습니다. 요소는 다른 요소를 포함 할 수 있습니다. 내가 스타일을 적용하기 위해 노력하고있어xslt 템플릿이 직접 자식 노드에는 적용되지 않습니다.

<?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 방법을 사용하여 나는, 내가 followinng XML 구조를 가지고 XSLT를 예를 들어 1.0

을 사용해야합니다. 그러나 직접적인 아이들에게 적용하는 대신 나는 평면을 보았습니다.

<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> 

어떻게 중첩 된 Grid에 셀을 배치합니까?

+0

당신이이 경우 예상 출력을 표시하도록 질문을 편집 할 수 있습니까? 감사! –

답변

1

"실패"(더 까다로운 상황이 있습니다) <Cell Col="0" Row="0"></Cell> [내부 격자의 일부]는 xsl:key을 통해 외부 격자의 동일한 @Row으로 일반화됩니다. 행 번호가 같지만 격자 깊이가 다릅니다.

그래서 당신은 그것들을 분리하고 독립적으로 만들어야합니다.

한 가지 방법 :

<?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> 
관련 문제