0

私はこのようなhtmlを持っています:

<table class="tbNoBorder" ..some attr here...>
<tr><td>1</td></tr><tr><td>2</td></tr>
</table>

<table ..some attr here... >
<tr><td>1</td></tr><tr><td>2</td></tr>
</table>

正規表現を使用して変換する必要があります

<table class="tbNoBorder" cellspacing="0" cellpadding="0" ..some attr here...>
<tr><td style="padding: 5px;">1</td></tr><tr><td style="padding: 5px;">2</td></tr>
</table>

<table cellspacing="0" cellpadding="0" ..some attr here... >
<tr><td style="border: solid 1px #ccc; padding: 5px;">1</td></tr><tr><td style="border: solid 1px #ccc; margin: 0; padding: 5px;">2</td></tr>
</table>

次に、それを Word に変換します。そのため、この変換が必要です。クラスtbNoBorderを持つテーブルには境界線があってはなりません。

これを行うためにこのコードを書きましたが、すべてのテーブルには罫線が付いています。最初の正規表現はすべてのテーブルを取得します。それを機能させるためのアイデアはありますか?

        //Fixes tables with borders
        content = Regex.Replace(content,
            @"<table(.*?)(?!tbNoBorder)(.*?)>(.*?)</table>",
            m =>
                {
                    var tableContent = Regex.Replace(m.Groups[3].ToString(), 
                                        @"<td",
                                        t => "<td style=\"border: solid 1px #ccc; padding: 5px;\"", RegexOptions.IgnoreCase
                                        );
                    return "<table cellspacing=\"0\" cellpadding=\"0\"" + m.Groups[1] + m.Groups[2] + ">" + tableContent + "</table>";
                }, RegexOptions.IgnoreCase
            );

        //Fixes tables without borders, has class tbNoBorder
        content = Regex.Replace(content,
            @"<table(.*?)tbNoBorder(.*?)>(.*?)</table>",
            m =>
            {
                var tableContent = Regex.Replace(m.Groups[3].ToString(),
                                    @"<td",
                                    t => "<td style=\"padding: 5px;\"", RegexOptions.IgnoreCase
                                    );
                return "<table cellspacing=\"0\" cellpadding=\"0\" + m.Groups[1] + m.Groups[2] + ">" + tableContent + "</table>";
            }, RegexOptions.IgnoreCase
        );
4

2 に答える 2

3

最初の正規表現をに変更します

@"<table(?![^>]*tbNoBorder)(.*?)>(.*?)</table>"

tbNoBorder開始タグにが含まれていると失敗します

于 2012-04-16T07:49:19.433 に答える
1

xslt を使用すると、次のように解決できます。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="table">
        <xsl:element name="table">
            <xsl:attribute name="cellspacing">0</xsl:attribute>
            <xsl:attribute name="cellpadding">0</xsl:attribute>
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:template>

    <xsl:template match="td">
        <xsl:element name="td">
            <xsl:if test="ancestor::table[not(@class='tbNoBorder')][1]">
                <xsl:attribute name="style">border: solid 1px #ccc; padding: 5px;</xsl:attribute>
            </xsl:if>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

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

</xsl:stylesheet>
于 2012-04-16T12:13:49.870 に答える