拆分合并单元格之间的值

时间:2015-03-25 11:55:20

标签: xslt

有人可以帮我解决下面的xslt问题。

我有5行的表。 前3个单元(1,2,3)合并,第一个单元有价值。 接下来的2个单元格(4,5)合并,第四个单元格有价值。

在这种情况下,我的xslt对于样本输入正常工作。如果单元格合并且第一个单元格有值,那么xslt正在工作。

但是,如果第四个单元格没有任何值,则xslt不能正常工作。

如何找到合并多少个单元格? 如果rowspan = 3,则合并三个单元格。此外,如果rowspan = 0,则此单元格与前一行合并。

如果rowapsn = 1,则单元格未合并。

如果rowmerged ='T',则单元格合并,rowmerged ='F'表示单元格未合并

样本输入:

<catalog>
<cd>
    <title rowmerge="F">Title 1</title>
    <artist rowmerge="F">sample 1</artist>
    <price rowmerge="T" rowspan="3" >1</price>
    <year >1985</year>
</cd>
<cd>
     <title rowmerge="F">Title 1</title>
    <artist rowmerge="F">sample 1</artist>
    <price rowmerge="T" rowspan="0"></price>
    <year >1985</year>
</cd>
<cd>
    <title rowmerge="F">Title 3</title>
    <artist rowmerge="F">Sample 3</artist>
    <price rowmerge="T" rowspan="0"></price>
    <year>1988</year>
</cd>
<cd>
    <title rowmerge="T">Title 4</title>
    <artist rowmerge="F">sample 4</artist>
    <price rowmerge="T" rowspan="2"></price>
    <year >1985</year>
  </cd>
  <cd>
    <title rowmerge="T"></title>
    <artist rowmerge="F">Sample 5</artist>
    <price rowmerge="T" rowspan="0"></price>
    <year>1988</year>
  </cd>
  </catalog>

预期输出:

<?xml version="1.0"?>
<catalog>
<cd>
<title rowmerge="f">Title 1</title>
<artist rowmerge="f">sample 1</artist>
<price rowmerge="F" rowspan="3">1</price>
<year>1985</year>
</cd>
<cd>
<title rowmerge="f">Title 1</title>
<artist rowmerge="f">sample 1</artist>
<price rowmerge="F" rowspan="0">1</price>
<year>1985</year>
</cd>
<cd>
<title rowmerge="F">Title 3</title>
<artist rowmerge="F">Sample 3</artist>
<price rowmerge="F" rowspan="0">1</price>
<year>1988</year>
</cd>
<cd>
<title rowmerge="F">Title 4</title>
<artist rowmerge="F">sample 4</artist>
<price rowmerge="F" rowspan="2"/>
<year>1985</year>
</cd>
<cd>
<title rowmerge="F"/>
<artist rowmerge="F">Sample 5</artist>
<price rowmerge="F" rowspan="0"/>
<year>1988</year>
</cd>
</catalog>

尝试使用以下xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="*[@rowmerge='T'][not(normalize-space())] [@rowspan= 0] ">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:value-of select="(../preceding-sibling::*/*[name() = name(current())][normalize-space()])[last()]/node()" />
        </xsl:copy>
    </xsl:template>


    <xsl:template match="@rowmerge[. = 'T']">
        <xsl:attribute name="rowmerge">F</xsl:attribute>
    </xsl:template>

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

我得到的是什么:

<?xml version="1.0"?>
<catalog>
<cd>
<title rowmerge="f">Title 1</title>
<artist rowmerge="f">sample 1</artist>
<price rowmerge="F" rowspan="3">1</price>
<year>1985</year>
</cd>
<cd>
<title rowmerge="f">Title 1</title>
<artist rowmerge="f">sample 1</artist>
<price rowmerge="F" rowspan="0">1</price>
<year>1985</year>
</cd>
<cd>
<title rowmerge="F">Title 3</title>
<artist rowmerge="F">Sample 3</artist>
<price rowmerge="F" rowspan="0">1</price>
<year>1988</year>
</cd>
<cd>
<title rowmerge="F">Title 4</title>
<artist rowmerge="F">sample 4</artist>
<price rowmerge="F" rowspan="2"/>
<year>1985</year>
</cd>
<cd>
<title rowmerge="F"/>
<artist rowmerge="F">Sample 5</artist>
<price rowmerge="F" rowspan="0">1</price>
<year>1988</year>
</cd>
</catalog>

1 个答案:

答案 0 :(得分:1)

您可以通过对其中一个谓词进行少量更改来实现此目的:

<xsl:template match="*[@rowmerge='T'][not(normalize-space())] [@rowspan= 0] ">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:value-of select="(../preceding-sibling::*/*[name() = name(current())]
                                                        [@rowspan != 0])[last()]" />
    </xsl:copy>
</xsl:template>

我已将[normalize-space()]更改为[@rowspan != 0] - &#34; head&#34;此范围的单元格不一定是具有非空值的最接近的前一个单元格,但它始终是最接近的前一个单元格,且非空rowspan

(我还从/node()的末尾删除了多余的value-of