CSS样式和XSLT? [英] CSS style and XSLT?

查看:74
本文介绍了CSS样式和XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在使用XSLT的XHTML文件中选择DIV标签,例如 // * [@ id ='background'] 如何添加样式,例如背景色或其他CSS样式(例如DIV的边框)?如果我在DIV ID = background中有一个列表,该如何设置列表的样式,例如删除项目符号? :)

If I select a DIV tag in a XHTML file with XSLT, like //*[@id='background'] How do I add a style, like a background color or other CSS styles like borders to the DIV? And if I have a list inside the DIV ID=background, how can I style the list, like removing the bullets? :)

推荐答案

使用XSLT确实很容易。例如,您的输入是:

This is really easy with XSLT. For instance, your input is:

<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,并覆盖感兴趣的节点:

You can use the identity transform to copy the input XML as is, and override the nodes of interest:

<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>

这篇关于CSS样式和XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆