2012-07-11 39 views
0
<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表格單元格取決於屬性值

我的XSLT的這部分做一個表中的行。

的列是../@e,../@b,@be,@o

對於每個A元素,我在表中創建一個行。

第3列的單元格有白色背景顏色無論如何。

所以: 首先前兩列未滿並且只有第一個兩列的第一個單元格必須已滿。所有其他的細胞都是空的。

但是如果任何A元素中至少有一個屬性爲真,則前2列(1ST行)的單元格必須是黃色的,以表明存在@i是真的。

請幫我弄明白。最近幾天它變成了一場噩夢。 預先感謝您。

完整的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> 
+0

你能插入XML源的改造,以及完整的XSLT? – 2012-07-11 08:07:23

+0

@YvesB我插入了它。希望你的建議:\我迷路了! – 2012-07-11 08:21:23

+0

恐怕您還需要提供預期的HTML,因爲您的解釋不容易理解。 – 2012-07-11 09:37:56

回答

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="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> 
+0

@YvesB先生我知道我問的太多了,但如果你有時間和意願,我希望你能進一步幫助我。 您的回答正是我所要求的。但是我有的真正的問題有更多的要求。其中之一是: - 如果在IRO/P元素處至少有一個i = True,並且在IRO/IRO/P元素處至少有一個,那麼../@n的顏色是紅色 - 如果在IRO/P元素處至少存在一個i = True,而不在任何IRO/IRO/P元素處,則../@n的顏色爲黃色 – 2012-07-11 13:26:01

+0

- 如果在IRO/P元素,但是在任何IRO/IRO/P元素中至少有一個i = True,那麼../@n的顏色是ORANGE - 如果IRO/P元素不是i = True,而不是IRO/IRO/P元素,然後是...的顏色。/ @ n是灰色的 我試圖用後代屬性來做,但我失敗了:( – 2012-07-11 13:26:20

+0

您是否有一個具有不同條件的輸入XML,以便我們可以看到更新後的XSLT是否有效? – 2012-07-11 13:34:31

0

通過附加reqts:

<?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元素的模板必須區分

相關問題