XSLT预示着复杂的情况

时间:2017-03-07 09:18:33

标签: html xml xslt foreach

我想使用xslt将下面的xml转换为html。

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>

1 个答案:

答案 0 :(得分:2)

看起来像我的另一个分组问题。 它假设@horder和@vorder都未排序。

你的例子中的@horder是排序的,这意味着你可以摆脱内循环

XSLT 2.0解决方案

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

XSLT 1.0 我使用了Muenchien分组,但请注意@vorder在整个数据集中并不是唯一的,所以我将它与父节点的generate-id连接起来。

<?php $link = $_SERVER["SERVER_NAME"].str_replace(".php","",$_SERVER["PHP_SELF"])."?name=".$name; ?>
<a class="magic" href="whatsapp://send?text=This year Wish Happy Holi to your friends and their family in different way :) . Click on link and receive my Happy Holi Wishes :) Enter your name and send your Wishes.%0A👇ðŸ¾ðŸ‘‡ðŸ¾ðŸ‘‡ðŸ¾ðŸ‘‡ðŸ¾ %0Ahttp://www.themobilesapp.com/<?= $link ?>" data-action="share/whatsapp/share">Share on Whatsapp</a>