2011-06-21 14 views
2

//*[@id='background']のように、XSLTを使用したXHTMLファイルでDIVタグを選択すると、背景色やDIVの枠線などの他のCSSスタイルのようなスタイルを追加するにはどうすればよいですか? DIV ID =背景内にリストがある場合、箇条書きを削除するなど、リストのスタイルを設定するにはどうすればよいですか? :)CSSスタイルとXSLT?

+0

+1すてきな質問です。 –

答えて

4

これはXSLTでは本当に簡単です。例えば、あなたの入力は次のとおりです。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
    <head> 
     <title></title> 
    </head> 
    <body> 
     <div id="background"> 
      <ul style="list-style-type: bullet"> 
       <li>Coffee</li> 
       <li>Tea</li> 
       <li>Milk</li> 
      </ul>  
     </div> 
    </body> 
</html> 

あなたはアイデンティティをそのまま入力XMLをコピーして変換し、関心のノードを無効に使用することができます。

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:x="http://www.w3.org/1999/xhtml" 
    exclude-result-prefixes="x"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="x:div[@id='background']"> 
     <xsl:copy> 
      <xsl:attribute name="style"> 
       <xsl:text>border-style:solid;border-width:medium</xsl:text> 
      </xsl:attribute> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="x:ul[ancestor::* 
     [name()='div' and @id='background']]/@style"> 
     <xsl:attribute name="style"> 
      <xsl:text>list-style-type: none</xsl:text> 
     </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 

出力は次のようになります。

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
    <head> 
     <title></title> 
    </head> 
    <body> 
     <div style="border-style:solid;border-width:medium" id="background"> 
     <ul style="list-style-type: none"> 
      <li>Coffee</li> 
      <li>Tea</li> 
      <li>Milk</li> 
     </ul> 
     </div> 
    </body> 
</html> 
+0

リストスタイルの更新を含むように更新されました。 –

+0

ありがとう!今までコードを見る時間がなかった。入力XHTMLをXSLTファイルに接続して、出力XHTMLを生成するにはどうすればよいですか。 XSLTリファレンスを入力の冒頭に置くためにプレーンなXMLを変換するときは好きですか? –

+0

この質問では、ブラウザの組み込みプロセッサを使用して変換を適用したいと考えています。右?スタイルシートへの参照を使用して、プレーンXMLを変換するときも、同じ方法で動作するはずです。 –