XSL:读取DIV类属性以创建HTML表属性

时间:2012-10-17 11:23:11

标签: xslt xslt-1.0

我需要使用XSLT 1.0将基于div的XHTML布局转换为基于表格的布局。对于基本转换,我有一个样式表(下面),可以很好地创建表结构。

我无法弄清楚如何在输入XHTML上解析多个类属性,以将特定于表的属性添加到输出中。 (是的,我希望这些作为新的表属性,即使类被复制)

我的示例XHTML是:

<div class="table align-center">
    <div class="tr">
        <div class="td"><p>Table Cell 1</p></div>
        <div class="td"><p>Table Cell 2</p></div>
    </div>
</div>

构建表结构的基本XSL OK,如下:

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

<xsl:template match="div[contains(@class, 'table')]">
    <table>
        <xsl:copy-of select="attribute::node()"/>
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="div[contains(@class, 'tr')]">
    <tr>
        <xsl:copy-of select="attribute::node()"/>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

<xsl:template match="div[contains(@class, 'td')]">
    <td>
        <xsl:copy-of select="attribute::node()"/>
        <xsl:apply-templates/>
    </td>
</xsl:template>

此样式表产生:

<table class="table align-center">
    <tr class="tr">
        <td class="td"><p>Table Cell 1</p></td>
        <td class="td"><p>Table Cell 2</p></td>
    </tr>
</table>

我想要制作的是:

<table class="table align-center" align="center">
    <tr class="tr">
        <td class="td"><p>Table Cell 1</p></td>
        <td class="td"><p>Table Cell 2</p></td>
    </tr>
</table>

是否可以使用XSLT 1.0执行此操作?我希望解决方案足够通用,可以添加2个或更多类,并解析它们以添加所需的表属性。

谢谢!

1 个答案:

答案 0 :(得分:1)

此XSLT 1.0样式表......

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*" />

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

  <xsl:template match="div[starts-with(@class, 'table ')]">
    <table>
        <xsl:call-template name="extract-class">
          <xsl:with-param name="class-list" select="normalize-space( substring-after(@class,'table '))" />
        </xsl:call-template>
        <xsl:apply-templates select="@*|node()"/>
    </table>
</xsl:template>

<xsl:template match="div[starts-with(@class, 'tr ')]">
    <tr>
        <xsl:apply-templates select="@*|node()"/>
    </tr>
</xsl:template>

<xsl:template match="div[starts-with(@class, 'td ')]">
    <td>
        <xsl:apply-templates select="@*|node()"/>
    </td>
</xsl:template>

<xsl:template name="extract-class">
  <xsl:param name="class-list" />
  <xsl:if test="contains($class-list,'-')">
    <xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" />
    <xsl:attribute name="{substring-before($name-value,'-')}">
      <xsl:value-of select="substring-after($name-value,'-')" />
    </xsl:attribute>
    <xsl:call-template name="extract-class">
      <xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" />
    </xsl:call-template> 
  </xsl:if>  
</xsl:template>

</xsl:stylesheet>

...应用于此文档时......

<div class="table align-center border-1 cellspacing-5">
    <div class="tr">
        <div class="td"><p>Table Cell 1</p></div>
        <div class="td"><p>Table Cell 2</p></div>
    </div>
</div>

<强> ...产量... *

<table align="center" border="1" cellspacing="5" class="table align-center border-1 cellspacing-5">
  <tr class="tr">
    <td class="td">
      <p>Table Cell 1</p>
    </td>
    <td class="td">
      <p>Table Cell 2</p>
    </td>
  </tr>
</table>
相关问题