【问题标题】:XSLT table cells depends on attribute valueXSLT 表格单元格取决于属性值
【发布时间】:2012-07-11 08:00:31
【问题描述】:
<xsl:for-each select="A">
<tr>            

<xsl:choose>
<xsl:when test="@a='True'">
<td bgcolor="#FFFF00"><xsl:value-of select="../@e"/></td>
<td bgcolor="#FFFF00"><xsl:value-of select="../@b"/></td>
<td bgcolor="#999999"><xsl:value-of select="@be"/></td>
<xsl:for-each select="T">
<td bgcolor="#FFFF00"><xsl:value-of select="@o"/></td>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<td bgcolor="#999999"><xsl:value-of select="../@e"/></td>
<td bgcolor="#999999"><xsl:value-of select="../@b"/></td>
<td bgcolor="#999999"><xsl:value-of select="@be"/></td>
<xsl:for-each select="T">
<td bgcolor="#999999"><xsl:value-of select="@o"/></td>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>

</tr>

我的 xslt 的这一部分组成了表格的行。

这些列是 ../@e、../@b、@be、@o

对于每个 A 元素,我在表格中创建一行。

第三列的单元格无论如何都有白色背景色。

所以: 首先,前 2 列未满,只有前两列的第一个单元格必须满。它们的所有其他单元格都是空的。

BUT 如果任何 A 元素中至少有一个 @i 属性为 True,则前 2 列(第 1 行)的单元格必须为黄色以表示存在 @我是真的。

请帮我弄清楚。在过去的几天里,它变成了一场噩梦。 提前谢谢你。

完整的 XSLT 转换如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<!-- param values may be changed during the XSL Transformation -->
<xsl:param name="shared_item_name"> Animal </xsl:param>
<xsl:param name="description"> Birth </xsl:param>
<xsl:param name="properties"> Kind </xsl:param>

<xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Problem</title>
        </head>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>


<xsl:template match="AA">
    <table border="1" cellspacing="0">
        <tr bgcolor="#9acd32">
            <th> <xsl:value-of select="$shared_item_name" /> </th>
            <th> <xsl:value-of select="$description" /> </th>
            <th> <xsl:value-of select="$properties" /> </th>
        <xsl:for-each select="IRO[position()=1]/P[position()=1]/T">
            <th> <xsl:value-of select="@s" /> </th>
        </xsl:for-each>
        </tr>               

<xsl:for-each select="AI">

    <xsl:for-each select="A">

        <tr>

        <xsl:if test="position()=1">
        <td><xsl:value-of select="../@e"/></td>
        <td bgcolor="#999999"><xsl:value-of select="../@b"/></td>
        </xsl:if>

        <xsl:if test="not(position()=1)">
        <td></td>
        <td></td>
        </xsl:if>

        <td bgcolor="#999999"><xsl:value-of select="@be"/></td>

        <xsl:choose>
        <xsl:when test="@a='True'">
        <xsl:for-each select="T">
        <td bgcolor="#FFFF00"><xsl:value-of select="@o"/></td>
        </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
        <xsl:for-each select="T">
        <td bgcolor="#999999"><xsl:value-of select="@o"/></td>
        </xsl:for-each>
        </xsl:otherwise>
        </xsl:choose>

        </tr>

    </xsl:for-each>

    <xsl:for-each select="AI">
    <xsl:for-each select="A">

        <tr>            

        <xsl:choose>
        <xsl:when test="@i='True'">
        <td bgcolor="#FFFF00"><xsl:value-of select="../@e"/></td>
        <td bgcolor="#FFFF00"><xsl:value-of select="../@b"/></td>
        <td bgcolor="#999999"><xsl:value-of select="@be"/></td>
        <xsl:for-each select="T">
        <td bgcolor="#FFFF00"><xsl:value-of select="@o"/></td>
        </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
        <td bgcolor="#999999"><xsl:value-of select="../@e"/></td>
        <td bgcolor="#999999"><xsl:value-of select="../@b"/></td>
        <td bgcolor="#999999"><xsl:value-of select="@be"/></td>
        <xsl:for-each select="T">
        <td bgcolor="#999999"><xsl:value-of select="@o"/></td>
        </xsl:for-each>
        </xsl:otherwise>
        </xsl:choose>

        </tr>
    </xsl:for-each>

    </xsl:for-each>

    </xsl:for-each>

</table>
</xsl:template>
    </xsl:stylesheet>

【问题讨论】:

  • 能否插入用于转换的 XML 源代码和完整的 XSLT?
  • @YvesB 我插入了它。希望您的建议:\我迷路了!
  • 恐怕您还需要提供预期的 HTML,因为您的解释不容易理解。
  • 天啊,先生。 @YvesB 非常感谢!!!!!!我现在没有言语可以表达我的幸福!谢谢 -> 00 :D

标签: xml xslt html-table


【解决方案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="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:param name="shared_item_name"> Shared Item Name </xsl:param>
<xsl:param name="description"> Description </xsl:param>
<xsl:param name="properties"> Properties </xsl:param>

<xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Problem</title>
        </head>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>


<xsl:template match="IR">
    <table border="1" cellspacing="0">
        <tr bgcolor="#9acd32">
            <th> <xsl:value-of select="$shared_item_name" /> </th>
            <th> <xsl:value-of select="$description" /> </th>
            <th> <xsl:value-of select="$properties" /> </th>
        <xsl:for-each select="IRO[1]/P[1]/T">
            <th> <xsl:value-of select="@s" /> </th>
        </xsl:for-each>
        </tr>               
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="P">
    <tr>
        <xsl:choose>
            <xsl:when test='count(preceding-sibling::P)=0'>
                <xsl:variable name='c'>
                    <xsl:choose>
                        <xsl:when test='(@i="True") or (count(following-sibling::P[@i="True"]) &gt; 0)'>#FFFF00</xsl:when>
                        <xsl:otherwise>#999999</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <td><xsl:attribute name='bgcolor'><xsl:value-of select='$c' /></xsl:attribute><xsl:value-of select='../@n' /></td>
                <td><xsl:attribute name='bgcolor'><xsl:value-of select='$c' /></xsl:attribute></td>
                <td bgcolor="#999999"><xsl:value-of select='@dn' /></td>
            </xsl:when>
            <xsl:otherwise>
                <td></td>
                <td></td>
                <td bgcolor="#999999"><xsl:value-of select='@dn' /></td>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates />
    </tr>
</xsl:template>    

<xsl:template match="T">
    <td>
        <xsl:attribute name='bgcolor'>
            <xsl:choose>
                <xsl:when test='../@i="True"'>#FFFF00</xsl:when>
                <xsl:otherwise>#999999</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
        <xsl:value-of select='@v' />
    </td>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 先生。 @YvesB 我知道我的要求太多了,但是如果您有时间和意愿,我希望您能进一步帮助我。你的回答正是我所要求的。但我遇到的真正问题有更多的要求。其中之一是:-如果在 IRO/P 元素处至少有一个 i=True 并且在 IRO/IRO/P 元素处至少有一个,则 ../@n 的颜色为红色 -如果在至少有一个 i=True 在 IRO/P 元素处,而不是在任何 IRO/IRO/P 元素处,则 ../@n 的颜色为 YELLOW
  • -如果在 IRO/P 元素中没有 i=True,但在任何 IRO/IRO/P 元素中至少有一个 i=True,则 ../@n 的颜色为橙色 - 如果在 IRO/P 元素中没有 i=True 而在 IRO/IRO/P 元素中没有,则 ../@n 的颜色为灰色 我尝试使用 descendant 属性执行此操作,但我失败了: (
  • 您是否有一个具有不同条件的输入 XML,以便我们查看更新后的 XSLT 是否有效?
  • 谢谢!!!!现在我必须努力学习,了解它的每一个细节!你真是太好了@YvesB 先生 :))) 它工作得很好!
【解决方案2】:

附加要求:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:param name="shared_item_name"> Shared Item Name </xsl:param>
<xsl:param name="description"> Description </xsl:param>
<xsl:param name="properties"> Properties </xsl:param>

<xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
            <title>Problem</title>
        </head>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>


<xsl:template match="IR">
    <table border="1" cellspacing="0">
        <tr bgcolor="#9acd32">
            <th> <xsl:value-of select="$shared_item_name" /> </th>
            <th> <xsl:value-of select="$description" /> </th>
            <th> <xsl:value-of select="$properties" /> </th>
        <xsl:for-each select="IRO[1]/P[1]/T">
            <th> <xsl:value-of select="@s" /> </th>
        </xsl:for-each>
        </tr>               
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="/IR/IRO/P">
    <tr>
        <xsl:choose>
            <xsl:when test='count(preceding-sibling::P)=0'>
                <xsl:variable name='c'>
                    <xsl:choose>
                        <xsl:when test='(@i="True") or (count(following-sibling::P[@i="True"]) &gt; 0)'>
                            <xsl:choose>
                                <xsl:when test='count(../descendant::P[@i="True"]) &gt; 0'>#ff0000</xsl:when>
                                <xsl:otherwise>#FFFF00</xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:choose>
                                <xsl:when test='count(../descendant::P[@i="True"]) &gt; 0'>#FF6600</xsl:when>
                                <xsl:otherwise>#999999</xsl:otherwise>
                            </xsl:choose>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <td><xsl:attribute name='bgcolor'><xsl:value-of select='$c' /></xsl:attribute><xsl:value-of select='../@n' /></td>
                <td><xsl:attribute name='bgcolor'><xsl:value-of select='$c' /></xsl:attribute></td>
                <td bgcolor="#999999"><xsl:value-of select='@dn' /></td>
            </xsl:when>
            <xsl:otherwise>
                <td></td>
                <td></td>
                <td bgcolor="#999999"><xsl:value-of select='@dn' /></td>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates />
    </tr>
</xsl:template>    

<xsl:template match="/IR/IRO/IRO/P">
    <tr>
        <xsl:choose>
            <xsl:when test='count(preceding-sibling::P)=0'>
                <xsl:variable name='c'>
                    <xsl:choose>
                        <xsl:when test='(@i="True") or (count(following-sibling::P[@i="True"]) &gt; 0)'>#FFFF00</xsl:when>
                        <xsl:otherwise>#999999</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <td><xsl:attribute name='bgcolor'><xsl:value-of select='$c' /></xsl:attribute><xsl:value-of select='../@n' /></td>
                <td><xsl:attribute name='bgcolor'><xsl:value-of select='$c' /></xsl:attribute></td>
                <td bgcolor="#999999"><xsl:value-of select='@dn' /></td>
            </xsl:when>
            <xsl:otherwise>
                <td></td>
                <td></td>
                <td bgcolor="#999999"><xsl:value-of select='@dn' /></td>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates />
    </tr>
</xsl:template>    

<xsl:template match="T">
    <td>
        <xsl:attribute name='bgcolor'>
            <xsl:choose>
                <xsl:when test='../@i="True"'>#FFFF00</xsl:when>
                <xsl:otherwise>#999999</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
        <xsl:value-of select='@v' />
    </td>
</xsl:template>

</xsl:stylesheet>

在这种情况下,必须区分 2 级 P 元素的模板

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2011-09-04
    • 2018-03-15
    相关资源
    最近更新 更多