2012-10-17 52 views
1

我需要使用XSLT 1.0将基于div的XHTML布局转换为基于表格的布局。对于基本的转换,我有一个样式表(见下)创建表结构很好。XSL:读取DIV类属性以创建HTML表格属性

我找不出如何解析输入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是建立表结构确定,如下:

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

我想什么produc e是这样的:

<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个或更多的类并解析它们以添加所需的表属性。

谢谢!

+0

类属性值要求标记化,所以我想知道您是否可以使用http://www.exslt.org/str/functions/tokenize/index.html。你的目标是哪个XSLT 1.0处理器? –

+0

嗨马丁。我使用PHP内置的XSL流程。我相信功能不受支持。 – Kevin

回答

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

哇!那很完美。非常感谢! – Kevin

+0

您的欢迎。请点击复选标记接受答案。 –