XSL文件为3列创建分组表

时间:2014-12-20 22:25:59

标签: java xslt

我遇到以下问题:我通过反映一个类的java创建这个xml文件。下面是一个xml文件的例子:

<ShoppingLists>
  <ShoppigList>
      <SLNAME>List #1</SLNAME>
      <Store>
        <SNAME>Store 1</SNAME>
        <VEGETABLES>Carrots,Lettuce,Onions</VEGETABLES>
        <FRUITS>Grapes,Oranges</FRUITS>
        <MEATS>Beef</MEATS>
        <DRINKS></DRINKS>
      </Store>
      <Store>
        <name>Store 3</name>
        <VEGETABLES></VEGETABLES>
        <FRUITS></FRUITS>
        <MEATS></MEATS>
        <DRINKS>Beer</DRINKS>
      </Store>
  </ShoppigList>
  <ShoppigList>
      <SLNAME>List #2</SLNAME>
      <Store>
        <SNAME>Store 5</SNAME>
        <VEGETABLES></VEGETABLES>
        <FRUITS></FRUITS>
        <MEATS>Fish</MEATS>
        <DRINKS></DRINKS>
      </Store>
      <Store>
        <SNAME>Store 12</SNAME>
        <VEGETABLES>Cucumbers</VEGETABLES>
        <FRUITS>Peaches</FRUITS>
        <MEATS></MEATS>
        <DRINKS>Water</DRINKS>
      </Store>
  </ShoppigList>
<ShoppingList>

我开发的我的xsl文件如下所示:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

        <xsl:variable name="empty_string"/>

        <xsl:template match="/">
            <html>
                <body>
                    <h2>Master Shopping List</h2>
                    <table border="1">
                        <tr bgcolor="#9acd32">
                            <th>Shopping List</th>
                            <th>Store</th>
                            <th>Items</th>
                        </tr>
                        <xsl:for-each select="ShoppingLists/ShoppingList">
                            <tr>
                                <td>
                                    <xsl:apply-templates select="SLNAME"/>
                                </td>
                                <xsl:for-each select="/Store">
                                    <td>
                                        <xsl:apply-templates select="STORENAME"/>
                                    </td>
                                    <td>
                                        <xsl:apply-templates select="VEGETABLES"/>  
                                        <xsl:apply-templates select="FRUITS"/>
                                        <xsl:apply-templates select="MEATS"/>
                                        <xsl:apply-templates select="DRINKS"/>
                                    </td>
                                </xsl:for-each>
                            </tr>
                        </xsl:for-each>
                    </table>
                </body>
            </html>
        </xsl:template>

        <xsl:template match="SLNAME">
            <span style="color:#000000">
                <xsl:value-of select="."/></span>
            <br />
        </xsl:template>

        <xsl:template match="STORENAME">
            <span style="color:#000000">
                <xsl:value-of select="."/></span>
            <br />
        </xsl:template>

        <xsl:template match="VEGETABLES">
            <xsl:choose>
                <xsl:when test="* or normalize-space()">
                    Vegetables: <span style="color:#000000">
                        <xsl:value-of select="."/>
                    </span>
                    <br />
                </xsl:when>
            </xsl:choose>
        </xsl:template>
        <xsl:template match="FRUITS">
            <xsl:choose>
                <xsl:when test="* or normalize-space()">
                    Frtuis: <span style="color:#000000">
                        <xsl:value-of select="."/>
                    </span>
                    <br />
                </xsl:when>
            </xsl:choose>
        </xsl:template>

        <xsl:template match="MEATS">
            <xsl:choose>
                <xsl:when test="* or normalize-space()">
                    Meast: <span style="color:#000000">
                        <xsl:value-of select="."/>
                    </span>
                    <br />
                </xsl:when>
            </xsl:choose>
        </xsl:template>

        <xsl:template match="DRINKS">
            <xsl:choose>
                <xsl:when test="* or normalize-space()">
                    Drinks: <span style="color:#000000">
                        <xsl:value-of select="."/>
                    </span>
                    <br />
                </xsl:when>
            </xsl:choose>
        </xsl:template>

    </xsl:stylesheet>

我在创建表格时遇到问题,如下所示:

Shopping List |  Store     | Items
------------------------------------
Shopping List | Store Name | Items | 
Name          |            |       |
              |--------------------|
              | Store Name | Items |
              |            |       |
------------------------------------

任何人都可以帮助我吗?感谢。

2 个答案:

答案 0 :(得分:1)

我猜(!)你想做的事情如下:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <table border="1">
        <tr>
            <th>Shopping List</th>
            <th>Store</th>
            <th>Items</th>
        </tr>
        <xsl:apply-templates select="ShoppingLists/ShoppingList" />         
    </table>
</xsl:template>

<xsl:template match="ShoppingList">
    <tr>
        <td rowspan="{count(Store)}">
            <xsl:value-of select="SLNAME"/>
        </td>
        <td>
            <xsl:value-of select="Store[1]/SNAME"/>
        </td>
        <td>
            <xsl:apply-templates select="Store[1]/*[string()]" /> 
        </td>           
    </tr>
    <xsl:apply-templates select="Store[position() > 1]" />                      
</xsl:template>

<xsl:template match="Store">
    <tr>
        <td><xsl:value-of select="SNAME"/></td>
        <td>
            <xsl:apply-templates select="*[string()]" /> 
        </td>                  
    </tr>
</xsl:template>

<xsl:template match="Store/*">
    <xsl:value-of select="local-name()"/>
    <xsl:text>: </xsl:text>
    <xsl:value-of select="."/>
    <br/>
</xsl:template>

<xsl:template match="SNAME" priority="1"/>

</xsl:stylesheet>

应用于更正的(!)输入示例:

<ShoppingLists>
  <ShoppingList>
      <SLNAME>List #1</SLNAME>
      <Store>
        <SNAME>Store 1</SNAME>
        <VEGETABLES>Carrots,Lettuce,Onions</VEGETABLES>
        <FRUITS>Grapes,Oranges</FRUITS>
        <MEATS>Beef</MEATS>
        <DRINKS></DRINKS>
      </Store>
      <Store>
        <SNAME>Store 3</SNAME>
        <VEGETABLES></VEGETABLES>
        <FRUITS></FRUITS>
        <MEATS></MEATS>
        <DRINKS>Beer</DRINKS>
      </Store>
  </ShoppingList>
  <ShoppingList>
      <SLNAME>List #2</SLNAME>
      <Store>
        <SNAME>Store 5</SNAME>
        <VEGETABLES></VEGETABLES>
        <FRUITS></FRUITS>
        <MEATS>Fish</MEATS>
        <DRINKS></DRINKS>
      </Store>
      <Store>
        <SNAME>Store 12</SNAME>
        <VEGETABLES>Cucumbers</VEGETABLES>
        <FRUITS>Peaches</FRUITS>
        <MEATS></MEATS>
        <DRINKS>Water</DRINKS>
      </Store>
  </ShoppingList>
</ShoppingLists>

结果将是:

<?xml version="1.0" encoding="utf-8"?>
<table border="1">
   <tr>
      <th>Shopping List</th>
      <th>Store</th>
      <th>Items</th>
   </tr>
   <tr>
      <td rowspan="2">List #1</td>
      <td>Store 1</td>
      <td>VEGETABLES: Carrots,Lettuce,Onions<br/>FRUITS: Grapes,Oranges<br/>MEATS: Beef<br/>
      </td>
   </tr>
   <tr>
      <td>Store 3</td>
      <td>DRINKS: Beer<br/>
      </td>
   </tr>
   <tr>
      <td rowspan="2">List #2</td>
      <td>Store 5</td>
      <td>MEATS: Fish<br/>
      </td>
   </tr>
   <tr>
      <td>Store 12</td>
      <td>VEGETABLES: Cucumbers<br/>FRUITS: Peaches<br/>DRINKS: Water<br/>
      </td>
   </tr>
</table>

呈现为:

enter image description here

答案 1 :(得分:0)

输入XML中有三个错误/拼写错误 - 每个<ShoppigList>应为<ShoppingList><name>Store 3</name>应为<SNAME>Store 3</SNAME>,结束标记必须为{{} 1}}而不是</ShoppingLists>。如果更改,则按照调整后的XSLT

进行
<ShoppingLists>

当应用于调整后的输入时,XML产生输出

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:variable name="empty_string"/>
  <xsl:template match="/">
    <html>
        <body>
            <h2>Master Shopping List</h2>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th>Shopping List</th>
                    <th>Store</th>
                    <th>Items</th>
                </tr>
                <xsl:for-each select="ShoppingLists/ShoppingList">
                    <tr>
                        <td>
                            <xsl:apply-templates select="SLNAME"/>
                        </td>
                        <xsl:for-each select="Store">
                            <td>
                                <xsl:apply-templates select="SNAME"/>
                            </td>
                            <td>
                                <xsl:apply-templates select="VEGETABLES"/>
                                <xsl:apply-templates select="FRUITS"/>
                                <xsl:apply-templates select="MEATS"/>
                                <xsl:apply-templates select="DRINKS"/>
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
  </xsl:template>
  <xsl:template match="SLNAME">
    <span style="color:#000000">
        <xsl:value-of select="."/>
    </span>
    <br />
  </xsl:template>
  <xsl:template match="SNAME">
    <span style="color:#000000">
        <xsl:value-of select="."/>
    </span>
    <br />
</xsl:template>
<xsl:template match="VEGETABLES">
    <xsl:choose>
        <xsl:when test="* or normalize-space()">
            Vegetables: <span style="color:#000000">
                <xsl:value-of select="."/>
            </span>
            <br />
        </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="FRUITS">
    <xsl:choose>
        <xsl:when test="* or normalize-space()">
            Frtuis: <span style="color:#000000">
                <xsl:value-of select="."/>
            </span>
            <br />
        </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="MEATS">
    <xsl:choose>
        <xsl:when test="* or normalize-space()">
            Meast: <span style="color:#000000">
                <xsl:value-of select="."/>
            </span>
            <br />
        </xsl:when>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="DRINKS">
    <xsl:choose>
        <xsl:when test="* or normalize-space()">
            Drinks: <span style="color:#000000">
                <xsl:value-of select="."/>
            </span>
            <br />
        </xsl:when>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

进行调整后:

<html>
 <body>
  <h2>Master Shopping List</h2>
  <table border="1">
     <tr bgcolor="#9acd32">
        <th>Shopping List</th>
        <th>Store</th>
        <th>Items</th>
     </tr>
     <tr>
        <td><span style="color:#000000">List #1</span><br></td>
        <td><span style="color:#000000">Store 1</span><br></td>
        <td>
           Vegetables: <span style="color:#000000">Carrots,Lettuce,Onions</span><br>
           Frtuis: <span style="color:#000000">Grapes,Oranges</span><br>
           Meast: <span style="color:#000000">Beef</span><br></td>
        <td><span style="color:#000000">Store 3</span><br></td>
        <td>
           Drinks: <span style="color:#000000">Beer</span><br></td>
     </tr>
     <tr>
        <td><span style="color:#000000">List #2</span><br></td>
        <td><span style="color:#000000">Store 5</span><br></td>
        <td>
           Meast: <span style="color:#000000">Fish</span><br></td>
        <td><span style="color:#000000">Store 12</span><br></td>
        <td>
           Vegetables: <span style="color:#000000">Cucumbers</span><br>
           Frtuis: <span style="color:#000000">Peaches</span><br>
           Drinks: <span style="color:#000000">Water</span><br></td>
     </tr>
   </table>
  </body>
</html>

而不是

<xsl:for-each select="Store">
   <td>
     <xsl:apply-templates select="SNAME"/>
   </td>

并调整了匹配模式

<xsl:for-each select="/Store">
    <td>
       <xsl:apply-templates select="STORENAME"/>
    </td>

<xsl:template match="STORENAME">

更新:我已经注意到这不是完整的解决方案 - 每个购物清单的商店必须是每一行,购物清单必须有相应的行数 - 并且如果没有给出完整的解决方案,将在几分钟内更新答案。

更新:正如michael.hor257k给出的正确答案,没有必要进一步调整,但是有关输入XML和XSLT的一些调整的其他信息暂时保持这个半答案。