Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在xsl标记中使用class属性_Html_Xml_Xslt - Fatal编程技术网

Html 在xsl标记中使用class属性

Html 在xsl标记中使用class属性,html,xml,xslt,Html,Xml,Xslt,尝试搜索论坛,但没有找到我需要的,所以我想我会问。我是XSL新手,所以我几乎不知道如何解析它,但可以理解编写的内容,所以我认为这个问题相当简单,我只是没有线索,因为我以前从未使用过XSL。因此,请原谅我使用了奇怪/错误的术语 背景,即我从数据库中获取的XML,实际上是直截了当的,只使用一个简单的语句副本来复制节点,因为大多数都是纯html。这是返回给我的代码的示例部分,如下所示: <tbody> <tr> <

尝试搜索论坛,但没有找到我需要的,所以我想我会问。我是XSL新手,所以我几乎不知道如何解析它,但可以理解编写的内容,所以我认为这个问题相当简单,我只是没有线索,因为我以前从未使用过XSL。因此,请原谅我使用了奇怪/错误的术语

背景,即我从数据库中获取的XML,实际上是直截了当的,只使用一个简单的语句副本来复制节点,因为大多数都是纯html。这是返回给我的代码的示例部分,如下所示:

<tbody>
              <tr>
                <td class="Bold Label Center">1</td>
                <td class="Bold Label Stub Left">Area</td>
                <td class="Bold Right">3.68</td>
              </tr>
              <tr>
                <td class="Bold Label Center">2</td>
                <td Indentation="2" class="Bold Label Stub Left">United States</td>
                <td class="Bold Right">7.68</td>
          </tr>
             <tr>
                <td class="Bold Label Center">3</td>
                <td Indentation="6" class="Label Stub Left">Oregon</td>
                <td class="Right">2.7</td>                    
           </tr>
 etc....
           <tr>
             <td class="Label Center">23</td>
             <td Indentation="6" class="Label Stub Left">Portland&lt;sup&gt;1&lt;/sup&gt;</td>
            <td class="Right">12.345</td>
           </tr>
</tbody>

1.
地区
3.68
2.
美国
7.68
3.
俄勒冈
2.7
等
23
波特兰SUP1/sup
12.345
XSL


这当然是好的,我去掉了一些其他代码只是为了保持简单。但我不知道怎么做,就是让TD class属性成为TD标记的一部分。因此,我希望理想的输出是,例如,在第一行中,输出是:

<TR><TD align=center><B>1</B></TD><TD align=left><B>Area</B></TD>
<TD align=right>3.68</TD></TR>
1区域
3.68
现在,仅仅是拷贝的输出当然不会传递那些我不知道怎么做的属性

当然,我也希望缩进一些项目(可能是nbsp;或者是样式像素),并使上标标记正常工作


我相信这真的很简单,但这对我来说是全新的,我只是在转动我的轮子,什么地方都不去。任何帮助都将不胜感激,我的直觉是这非常简单。

您可以创建一个模板来选择
节点,并检查
属性是否包含
中心
。然后使用适当的
align
属性生成
td

根据您的示例,下面是一个简单的方法

将您的
副本
替换为
应用模板

<xsl:template match="/">
    <xsl:for-each select="div[@class='HtmlTable']/table">
        <table cellpadding="0" cellspacing="0">
            <xsl:apply-templates select="tbody"/>       
        </table>
    </xsl:for-each>
</xsl:template>
并为每个
td
align案例添加模板:

<xsl:template match="td[contains(@class, 'Center')]">
    <td align="center">
        <xsl:copy-of select="node()"/>
    </td>
</xsl:template>
<xsl:template match="td[contains(@class, 'Left')]">
    <td align="left">
        <xsl:copy-of select="node()"/>
    </td>
</xsl:template>
<xsl:template match="td[contains(@class, 'Right')]">
    <td align="right">
        <xsl:copy-of select="node()"/>
    </td>
</xsl:template>

这将生成一个包含
td
元素的表,该元素包含
align
属性,而不是类选择器


请参见此处的一个工作示例:

好吧,这确实很有效,我也将其用于粗体项目。现在如果我能想出如何动态使用缩进,我想我已经准备好了。。。无法使模板正常工作,并使用@indentation值作为像素*可以使用的样式填充常量。如果您在任何与
td
匹配的模板中,您可以使用类似
的内容,例如
@Indentation
6
,则会生成
。大括号允许在静态标记中嵌入XPath表达式(就像您在其中使用了一个
。它们被称为属性值模板。这不是唯一的解决方案。在XSLT中也有很多方法可以做同样的事情。您也可以对每个
if
使用
进行同样的操作,但通常最好使用带有条件选择的递归模板,使用XPath predic)ates用于非顺序循环和条件,并且很少对每个
if/choose
使用
。如果您可以花一些时间学习一些遵循这些函数式语言实践的XPath和XSLT,使用XSLT和XPath将变得容易得多。是的,我正在尝试实际上我似乎无法集成禁用输出-节点副本中的转义因子…它弄乱了我的标记并将其作为文本写入…嗯。。
<xsl:template match="tbody">
    <xsl:copy>
        <xsl:apply-templates select="tr"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="tr">
    <xsl:copy>
        <xsl:apply-templates select="td"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="td[contains(@class, 'Center')]">
    <td align="center">
        <xsl:copy-of select="node()"/>
    </td>
</xsl:template>
<xsl:template match="td[contains(@class, 'Left')]">
    <td align="left">
        <xsl:copy-of select="node()"/>
    </td>
</xsl:template>
<xsl:template match="td[contains(@class, 'Right')]">
    <td align="right">
        <xsl:copy-of select="node()"/>
    </td>
</xsl:template>