2017-03-01 55 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对不起..发表评论部分....☺