2017-03-01 54 views
0

我想要改造這個XMLXSLT轉換每個複雜條件

XML

<Page> 
    <Sections> 
    <Section id="parent_hdr_sec" rows="2" columns="1"> 
     <Sections> 
     <Section id="plan_hdr" rows="0" columns="0" /> 
     <Section id="plan_hdr_dtl" rows="0" columns="0" /> 
     </Sections> 
    </Section> 
    <Section id="splitter_sec" rows="1" columns="2"> 
     <Sections> 
     <Section id="parent_left_sec" rows="4" columns="1"> 
     </Section> 
     <Section id="parent_right_sec" rows="7" columns="1"> 
     </Section> 
     </Sections> 
    </Section> 
    </Sections> 
</Page> 

XSLT

<xsl:template match="/"> 
    <html> 
     <body> 
     <table> 
      <xsl:for-each select="Page/Sections/Section"> 
      <tr> 
       <td> 
       <xsl:attribute name="id"> 
        <xsl:value-of select="@id"/> 
       </xsl:attribute> 
       <table> 
        <xsl:variable name="Rows" select="@rows"/> 
        <xsl:variable name="Columns" select="@columns"/> 

        <xsl:for-each select="(//Section/Sections/Section)[$Rows >= position()]"> 
        <tr> 
         <xsl:for-each select="(//Section/Sections/Section)[$Columns >= position()]"> 
         <td> 
          <xsl:attribute name="id"> 
          <xsl:value-of select="@id"/> 
          </xsl:attribute> 
         </td> 
         </xsl:for-each> 
        </tr> 
        </xsl:for-each> 

       </table> 
       </td> 
      </tr> 
      </xsl:for-each> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

我的輸出

<html> 
    <body> 
    <table> 
     <tr> 
     <td id="parent_hdr_sec"> 
      <table> 
      <tr> 
       <td id="plan_hdr"></td> 
      </tr> 
      <tr> 
       <td id="plan_hdr"></td> 
      </tr> 
      </table> 
     </td> 
     </tr> 
     <tr> 
     <td id="splitter_sec"> 
      <table> 
      <tr> 
       <td id="plan_hdr"></td> 
       <td id="plan_hdr_dtl"></td> 
      </tr> 
      </table> 
     </td> 
     </tr> 
    </table> 
    </body> 
</html> 

正確的輸出

'ID' 變量值在我的輸出錯誤更新。

我需要輸出這樣的下面,

<html> 
    <body> 
    <table> 
     <tr> 
     <td id="parent_hdr_sec"> 
      <table> 
      <tr> 
       <td id="plan_hdr"></td> 
      </tr> 
      <tr> 
       <td id="plan_hdr_dtl"></td> 
      </tr> 
      </table> 
     </td> 
     </tr> 
     <tr> 
     <td id="splitter_sec"> 
      <table> 
      <tr> 
       <td id="parent_left_sec"></td> 
       <td id="parent_right_sec"></td> 
      </tr> 
      </table> 
     </td> 
     </tr> 
    </table> 
    </body> 
</html> 

我想XSLT每個條件得到上面的輸出。

誰能幫我解決這個問題..

回答

0

我真的不明白你試圖與$Rows$Columns變量完成的任務。有一點似乎很清楚,就是在每個分支中,您只需要處理屬於該分支的節點 - 因此您應該而不是開始您的選擇表達式與//

嘗試也許是:

<xsl:template match="/"> 
    <html> 
     <body> 
     <table> 
      <xsl:for-each select="Page/Sections/Section"> 
      <tr> 
       <td> 
       <xsl:attribute name="id"> 
        <xsl:value-of select="@id"/> 
       </xsl:attribute> 
       <table> 
        <xsl:variable name="Rows" select="@rows"/> 
        <xsl:variable name="Columns" select="@columns"/> 

        <xsl:for-each select="Sections[$Rows >= position()]"> 
        <tr> 
         <xsl:for-each select="Section[$Columns >= position()]"> 
         <td> 
          <xsl:attribute name="id"> 
          <xsl:value-of select="@id"/> 
          </xsl:attribute> 
         </td> 
         </xsl:for-each> 
        </tr> 
        </xsl:for-each> 

       </table> 
       </td> 
      </tr> 
      </xsl:for-each> 
     </table> 
     </body> 
    </html> 
</xsl:template> 

需要注意的是:

<td> 
    <xsl:attribute name="id"> 
     <xsl:value-of select="@id"/> 
    </xsl:attribute> 
    <!-- more --> 
</td> 

可以在短期內書面:

<td id="{@id}"> 
    <!-- more --> 
</td> 

見:https://www.w3.org/TR/xslt/#attribute-value-templates

+0

感謝您的回覆。我正在輸出這樣,如果使用上述的一個 – Hari

+0

Hari

+0

@Hari請不要張貼在評論的代碼,它是相當難以理解。 –

0

@Michael對不起..發表評論部分....☺