2017-03-07 78 views
-1

我想使用xslt將下面的xml轉換爲html。XSLT foreach複雜條件

horder - 表示元素

vorder的行位置 - 表示元素的列位置

行 - 總無。列中存在的行數

列 - 總數沒有。的

<Page> 
    <Sections> 
    <Section id="parent_hdr_sec" haschild="y" layout="hbox" rows="2" columns="1" horder="1" vorder="1"> 
     <Sections> 
     <Section id="plan_hdr" layout="vbox" rows="1" columns="6" horder="1" vorder="1" /> 
     <Section id="plan_hdr_dtl" layout="vbox" rows="1" columns="5" horder="2" vorder="1" /> 
     </Sections> 
    </Section> 
    <Section id="splitter_sec" haschild="y" layout="vbox" rows="1" columns="2" horder="2" vorder="1"> 
     <Sections> 
     <Section id="parent_left_sec" haschild="y" layout="hbox" rows="4" columns="1" horder="1" vorder="1" /> 
     <Section id="parent_right_sec" haschild="y" layout="hbox" rows="7" columns="1" horder="1" vorder="2" /> 
     <Section id="parent_left_sec_1" layout="hbox" rows="1" columns="6" horder="2" vorder="1" /> 
     <Section id="parent_right_sec_1" layout="hbox" rows="1" columns="5" horder="2" vorder="2" /> 
     </Sections> 
    </Section> 
    </Sections> 
</Page> 

需要HTML輸出出現在元素中列:

<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> 
      <tr> 
       <td id="parent_left_sec_1"></td> 
       <td id="parent_right_sec_1"></td> 
      </tr> 
      </table> 
     </td> 
     </tr> 
    </table> 
    </body> 
</html> 

請幫助我建立XSLT對上述一個...需要建議對此..

我的XSLT:

<xsl:template match="/"> 
    <html> 
     <body> 
     <table> 
      <xsl:for-each select="Page/Sections/Section"> 
      <tr> 
       <td id="{@id}"> 

       <table> 
        <xsl:variable name="Rows" select="@rows"/> 
        <xsl:variable name="Columns" select="@columns"/> 
        <xsl:variable name="Layout" select="@layout"/> 

        <xsl:if test="$Layout = 'hbox'"> 
        <xsl:for-each select="Sections/Section[$Rows >= position()]"> 
         <tr> 
         <td id="{@id}"> 
         </td> 
         </tr> 
        </xsl:for-each> 
        </xsl:if> 

        <xsl:if test="$Layout = 'vbox'"> 
        <tr> 
         <xsl:for-each select="Sections/Section[$Columns >= position()]"> 
         <td id="{@id}"> 
         </td> 
         </xsl:for-each> 
        </tr> 
        </xsl:if> 
       </table> 
       </td> 
      </tr> 
      </xsl:for-each> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

我用上面的XSLT使用foreach條件並得到像這樣的輸出

我的輸出:

<?xml version="1.0" encoding="utf-8"?> 
<html> 
    <body> 
    <table> 
     <tr> 
     <td id="parent_hdr_sec"> 
      <table> 
      <tr> 
       <td id="plan_hdr" /> 
      </tr> 
      <tr> 
       <td id="plan_hdr_dtl" /> 
      </tr> 
      </table> 
     </td> 
     </tr> 
     <tr> 
     <td id="splitter_sec"> 
      <table> 
      <tr> 
       <td id="parent_left_sec" /> 
       <td id="parent_right_sec" /> 
      </tr> 
      </table> 
     </td> 
     </tr> 
    </table> 
    </body> 
</html> 
+0

Downvoting,因爲你沒有解釋你卡在哪裏。你只是要求人們爲你寫代碼,而不是解釋你爲什麼不能自己寫。 –

+0

@MichaelKay Im new to XSLT so so stucked here ... Sorry for not posting my XSLT code at ... – Hari

回答

2

看起來像另一個組的問題給我。 它假定@horder和@vorder都未排序。

在你的榜樣@horder進行排序,這意味着你可以擺脫內環

XSLT 2.0解決方案

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


<xsl:template match="//Page"> 
<html> 
<body> 
<xsl:apply-templates select="Sections" /> 
</body> 
</html> 
</xsl:template> 

<xsl:template match="Sections"> 
<table> 
    <xsl:for-each-group select="Section" group-by="@vorder"> 
      <xsl:sort select="@vorder"/> 
      <tr> 
       <xsl:for-each select="current-group()"> 
        <xsl:sort select="@horder"/> 
        <xsl:apply-templates select="current()" /> 
       </xsl:for-each> 
      </tr> 
     </xsl:for-each-group> 
</table> 
</xsl:template> 

<xsl:template match="Section"> 
<td> 
    <xsl:attribute name="id" select="@id" /> 
     <xsl:attribute name="horder" select="@horder" /> 
    <xsl:apply-templates select="Sections" /> 
</td> 
</xsl:template> 

</xsl:stylesheet> 

XSLT 1.0 我用Muenchien分組的,但要注意@vorder在整個數據集中不是唯一的,所以我將它與父節點的generate-id連接起來。

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

<xsl:key name="section-by-vorder" match="Section" use="concat(generate-id(parent::*), @vorder)" /> 

<xsl:template match="//Page"> 
<html> 
<body> 
<xsl:apply-templates select="Sections" /> 
</body> 
</html> 
</xsl:template> 

<xsl:template match="Sections"> 
<table> 
    <!-- select first Section from each group --> 

    <xsl:for-each select="Section[count(. | key('section-by-vorder', concat(generate-id(parent::*), @vorder))[1]) = 1]"> 
      <xsl:sort select="@vorder"/> 
      <tr> 
       <!-- for all contacts in a group --> 
       <xsl:for-each select="key('section-by-vorder', concat(generate-id(parent::*), @vorder))"> 
        <xsl:sort select="@horder"/> 
        <xsl:apply-templates select="current()" /> 
       </xsl:for-each> 
      </tr> 
     </xsl:for-each> 
</table> 
</xsl:template> 



<xsl:template match="Section"> 
<td id="{@id} horder={@horder} vorder={@vorder}"> 
    <xsl:apply-templates select="Sections" /> 
</td> 
</xsl:template> 

</xsl:stylesheet> 
+0

Thanks @Lesiak for replys.actually我xslt 1.0 ... so for each-group不被認可。您可以請在xslt 1.0中建議 – Hari

+0

@Hari請發佈您的XSLT代碼並描述您遇到的問題。我們不是一個代碼編寫服務,「建議」您可以複製和粘貼的解決方案。從你的結尾做出的超出*的一些努力就是我的輸入,這是我想要的輸出。*實際上是在這裏需要的。 – Tomalak

+0

@Tomalak對不起,在提問的時候沒有發佈我的XSLT代碼... – Hari