2016-06-21 7 views
0

私はこのxsltを使用していますが、テーブル行に別の色を表示するという問題があります。行の色が来なく代替行のための再帰の親タグがXSLTを使用してテーブル行をスタイルする方法

を完了した後にその影響を取得あなたは私

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

 
<xsl:strip-space elements="*"/> 
 
<xsl:template match="/"> 
 
    <html> 
 
    <body> 
 
    <table border="1"> 
 
    <xsl:apply-templates select="*"/> 
 
    </table> 
 
    </body> 
 
    </html> 
 
</xsl:template> 
 

 
<xsl:template match="*[text()]"> 
 

 
    <xsl:variable name="altColor"> 
 
    <xsl:choose> 
 
     <xsl:when test="position() mod 2 = 0">#FFFFFF</xsl:when> 
 
     <xsl:otherwise>#D3DFEE</xsl:otherwise> 
 
    </xsl:choose> 
 
    </xsl:variable> 
 
    
 
    <tr bgcolor="{$altColor}"> 
 
    <td> 
 
    <xsl:for-each select="ancestor-or-self::*"> 
 
     <xsl:value-of select="name()" /> 
 
     <xsl:if test="position()!=last()"> 
 
      <xsl:text>_</xsl:text> 
 
     </xsl:if> 
 
    </xsl:for-each> 
 
    </td> 
 
    <td> 
 
    <xsl:value-of select="." /> 
 
    </td> 
 
    </tr> 
 
    <xsl:text>&#10;</xsl:text> 
 
    <xsl:apply-templates select="*"/> 
 
</xsl:template> 
 
</xsl:stylesheet>

入力サンプルXMLは、この

ですが助けてくださいすることができています

<?xml-stylesheet type="text/xsl" href="xmltohtmlrecurnewfromAns.xsl"?> 
 
<A> 
 
    <B>Text</B> 
 
    <C>Text</C> 
 
    <D> 
 
     <D1>Text</D1> 
 
     <D2> 
 
      <D3>Text</D3> 
 
      <D4>Text</D4> 
 
     </D2> 
 
    </D> 
 
    <E> 
 
     <E1> 
 
      <E2> 
 
       <E3>Text</E3> 
 
      </E2> 
 
     </E1> 
 
    </E> 
 
</A>

答えて

1

<xsl:apply-templates select="*"/>を使用して再帰的に実行するのではなく、1つの選択ですべての「リーフ」要素を選択するだけです。

<xsl:apply-templates select="//*[text()]"/> 

このようにして、位置を確認するロジックが期待通りに機能します。

これは私が望んでいたとして働いてます。..あなたの助けのための

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:strip-space elements="*"/> 
<xsl:template match="/"> 
    <html> 
    <body> 
    <table border="1"> 
    <xsl:apply-templates select="//*[text()]"/> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:variable name="altColor"> 
    <xsl:choose> 
     <xsl:when test="position() mod 2 = 0">#FFFFFF</xsl:when> 
     <xsl:otherwise>#D3DFEE</xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 

    <tr bgcolor="{$altColor}"> 
    <td> 
    <xsl:for-each select="ancestor-or-self::*"> 
     <xsl:value-of select="name()" /> 
     <xsl:if test="position()!=last()"> 
      <xsl:text>_</xsl:text> 
     </xsl:if> 
    </xsl:for-each> 
    </td> 
    <td> 
    <xsl:value-of select="." /> 
    </td> 
    </tr> 
</xsl:template> 
</xsl:stylesheet> 
+0

おかげで、このXSLTをお試しください – user6496037

関連する問題