2017-01-13 2 views
0

xmlの属性の存在に基づいて行のbgcolorを変更します。各属性は、私がやろうとしています何xmlの属性の存在に基づいて行のbgcolorを変更します。各属性はこの行のセルになります

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="abc.xsl"?> 
<cars> 
<toyota petrol="true" color="red" abs="true" new-feature1="true"/> 
<mazda petrol="true" color="red" abs="true" new-feature2="true"/> 
</cars> 

この行のセルになりますと、私は何をしたいの車

<table> 
<tr>toyota</tr> 
<tr bgcolor="green"> 
<td>petrol=true</td> 
<td>color=red</td> 
<td>abs=true</td> 
<td>new-feature1=true</td> 
</tr> 
<tr>mazda</tr> 
<tr bgcolor="green"> 
<td>petrol=true</td> 
<td>color=red</td> 
<td>abs=true</td> 
<td>new-feature2=true</td> 
</tr> 
</table> 

とXSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
    <html> 
    <body> 
    <h2>cars</h2> 
    <table border="1"> 
     <xsl:for-each select="cars/*"> 
     <tr><xsl:value-of select="name()"/></tr> 
     <tr> 
     <xsl:for-each select="@*"> 
     <td><xsl:value-of select="name()"/>=<xsl:value-of select="."/></td> 
     </xsl:for-each> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 

のテーブルを作ることです"new-feature *"属性が存在する場合は行全体を緑色にし、そうでない場合は行を赤色にすることです

+0

質問を編集して現在のXSLTを表示できますか?ありがとうございました! –

+0

ok ...それは、xmlやxslのどちらも実際の実装ではないことに注意してください。わかりやすくするために、小さなxmlを作成しました。 –

答えて

1

nはxsl:if名前が始まり-で「新しい機能」し、新しい属性を作成するためにxsl:attributeを使用

<tr> 
    <xsl:if test="@*[starts-with(name(), 'new-feature')]"> 
     <xsl:attribute name="bgcolor">green</xsl:attribute> 
    </xsl:if> 
    <xsl:for-each select="@*"> 
    <td><xsl:value-of select="name()"/>=<xsl:value-of select="."/></td> 
    </xsl:for-each> 
    </tr> 

(注)このスニペットを試してみてください属性があるかどうかを確認するための条件、それは実際には良いかもしれませんCSSクラスを使用して色を定義します。これは潜在的にコードを単純化する可能性があるためです。

は注意

<xsl:template match="/"> 
    <html> 
    <head> 
     <style> 
      .new-feature-true 
      { 
      background-color: green; 
      } 
     </style> 
    </head> 
    <body> 
    <h2>cars</h2> 
    <table border="1"> 
     <xsl:for-each select="cars/*"> 
     <tr><xsl:value-of select="name()"/></tr> 
     <tr class="new-feature-{@*[starts-with(name(), 'new-feature')]}"> 
     <xsl:for-each select="@*"> 
     <td><xsl:value-of select="name()"/>=<xsl:value-of select="."/></td> 
     </xsl:for-each> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 

ここにも "クラス" 属性を生成するためにAttribute Value Templatesの使用を、このXSLTを試してみてください。

+0

は、魅力的に働いていました。D ..ありがとうございました。 xslの良いチュートリアルのページ? –

+0

http://stackoverflow.com/questions/3511759/where-can-i-find-a-good-tutorial-on-xslt-files/3512184#3512184には、XSLTを学ぶ方法の提案があります。 –

関連する問題