表行调整的XSL / HTML数

时间:2013-12-13 01:52:00

标签: html css xml xslt

我目前正在使用XML v1.0。我遇到了一个问题,我不能为每个“狗”创建一个新行。我附上了一个屏幕截图,以帮助想象我拥有的和结果。第一个截图是我的。第二个屏幕截图指向我正在尝试解决的问题。每只“狗”都应该在它自己的盒子里。

enter image description here enter image description here

我要做的是将“狗”列入其自己的表空间。我当前的代码是这样的:

        <xsl:for-each select="animal-list/list">
            <tr>
                <td>
                    <xsl:apply-templates select="cat-list"/>
                </td>
                <td>
                    <xsl:for-each select="dog-list">
                        <apply-templates select="dog">
                        <xsl:if test="position() != last()">
                            <br/>
                        </xsl:if>
                    </xsl:for-each>
                </td>
            </tr>   
        </xsl:for-each>

我尝试用各种不同的标签替换<br/>以尝试实现我的目标。但我没有尝试过任何工作。所以澄清一下我的问题是,如何在他们自己的小表数据/行中加入“Dog”,就像每个“Dog”之间有一条线

编辑:

这是XML数据:

                <animal-list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list> 
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                    </list>
                    <list>
                        <cat-list>Cat</cat-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                        <dog-list>Dog</dog-list>
                    </list>
                </animal-list>

2 个答案:

答案 0 :(得分:1)

没有XML意味着没有XSLT,所以我猜你的HTML问题比XSLT更多。您必须使用rowspan属性,例如:

<table border="1">
  <tr>
    <td rowspan="5">cat</td>
    <td>dog</td>
  </tr>
  <tr>
    <td>dog</td>
  </tr>
  <tr>
    <td>dog</td>
  </tr>
  <tr>
    <td>dog</td>
  </tr>
  <tr>
    <td>dog</td>
  </tr>
</table>

如何使用XSLT做到这一点是另一个没有XML就无法回答的问题。

<强>更新: 您可以尝试使用此xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
  <xsl:output omit-xml-declaration="yes" method="xml" indent="yes" />

<!-- template for root element: table element and apply-templates for cat-list elements -->
<xsl:template match="/">
  <table border="1">
    <xsl:apply-templates select="animal-list/list/cat-list"/>
  </table>
</xsl:template>

<!-- template for cat-list elements -->
<xsl:template match="cat-list">
  <!-- count all dog-list elements which are 'children of my parent node' -->
  <xsl:variable name="mysiblings" select="parent::list/dog-list" /><!-- this variable will contain all of mi dog-list siblings -->
  <xsl:variable name="myspan" select="count($mysiblings)" /><!-- this variable will contain the number of them -->

  <!-- for every cat-list: one row -->
  <tr>
    <td rowspan="{$myspan}">
      <xsl:value-of select="." /><!-- output my text -->
    </td>
    <!-- first dog-list: inside same tr -->
    <xsl:apply-templates select="$mysiblings[1]" />
  </tr>
  <!-- rest of them outside: they have their own tr: -->
  <xsl:apply-templates select="$mysiblings[position() &gt; 1]" />
</xsl:template>

<!-- template for dog-list elements, general -->
<xsl:template match="dog-list">
  <!-- tr and td  -->
  <tr>
    <td>
      <xsl:value-of select="." /><!-- output my text -->
    </td>
  </tr>
</xsl:template>

<!-- template for the first one. This one will go inside same tr as the cat -->
<xsl:template match="dog-list[position() = 1]">
  <!-- just td  -->
  <td>
    <xsl:value-of select="." /><!-- output my text -->
  </td>
</xsl:template>

</xsl:stylesheet>

希望它有所帮助。

答案 1 :(得分:1)

我会说,这是一些奇怪的输入。无论如何,这是一种看待它的方法。这是假设猫和狗的列彼此独立。

<?xml version="1.0" encoding="UTF-8"?>
<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="/">
<html>
<body>
<table>
    <tr>
        <th>Cats</th>
        <th>Dogs</th>
    </tr>

    <xsl:call-template name="createRows">
        <xsl:with-param name="cats" select="animal-list/list/cat-list"/>
        <xsl:with-param name="dogs" select="animal-list/list/dog-list"/>
    </xsl:call-template>

</table>
</body>
</html>
</xsl:template>

<xsl:template name="createRows">
    <xsl:param name="cats"/>
    <xsl:param name="dogs"/>
    <xsl:param name="i" select="1"/>

    <xsl:if test="$i &lt;= count($cats) or $i &lt;= count($dogs)">
        <tr>
            <td><xsl:value-of select="$cats[$i]"/></td>
            <td><xsl:value-of select="$dogs[$i]"/></td>
        </tr>
        <xsl:call-template name="createRows">
            <xsl:with-param name="cats" select="$cats"/>
            <xsl:with-param name="dogs" select="$dogs"/>
            <xsl:with-param name="i" select="$i+1"/>
        </xsl:call-template>
    </xsl:if>

</xsl:template>

</xsl:stylesheet> 

如果上述假设错误:

<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="/">
<html>
<body>
<table>
    <tr>
        <th>Cats</th>
        <th>Dogs</th>
    </tr>
    <xsl:for-each select="animal-list/list">
        <tr>
            <td>
                <xsl:if test="count(dog-list) > 1">
                    <xsl:attribute name="rowspan">
                        <xsl:value-of select="count(dog-list)"/>
                    </xsl:attribute>
                </xsl:if>
                <xsl:value-of select="cat-list"/>
            </td>   
            <td><xsl:value-of select="dog-list[1]"/></td>
        </tr>           
        <xsl:for-each select="dog-list[position() > 1]">
            <tr><td><xsl:value-of select="."/></td></tr>
        </xsl:for-each>
    </xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>