1

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>

テーブル構造OKを構築する基本的な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>

私が作りたいのはこれです:

<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つ以上のクラスを追加し、それらを解析して必要なテーブル属性を追加するのに十分な一般的なソリューションが必要です。

ありがとうございました!

4

1 に答える 1

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>
于 2012-10-17T12:12:39.297 に答える