为下面的场景编写xslt

时间:2015-01-23 08:35:30

标签: xslt

INPUT xml:

<?xml version="1.0"?>
<TABLE>
<THEAD>
    <ROW id="rh">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >A</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >B</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >C</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >D</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >F</Para>
        </CELL>
    </ROW>


</THEAD>
<TBODY editable="T">
    <ROW id="r1">
        <CELL rowmerged="T"  rowspan="2" >
            <Para >11</Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="2" >
            <Para >12</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >13</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >14</Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="2" >
            <Para >15</Para>
        </CELL>
    </ROW>

    <ROW id="r2">
        <CELL rowmerged="T"  rowspan="2" >
            <Para ></Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="2" >
            <Para ></Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >23</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >24</Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="2" >
            <Para ></Para>
        </CELL>
    </ROW>

    <ROW id="r3">
        <CELL rowmerged="T"  rowspan="2" >
            <Para ></Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="2" >
            <Para ></Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >33</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >34</Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="2" >
            <Para ></Para>
        </CELL>
    </ROW>

    <ROW id="r4">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >41</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >42</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >43</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >44</Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="1" >
            <Para >45</Para>
        </CELL>
    </ROW>
</TBODY>
</TABLE>

规则: 对于合并行:将主合并单元格的内容复制到合并行中的其他单元格。 预期结果:

<?xml version="1.0"?>
<TABLE>
<THEAD>
    <ROW id="rh">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >A</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >B</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >C</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >D</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >F</Para>
        </CELL>
    </ROW>


</THEAD>
<TBODY editable="T">
    <ROW id="r1">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >11</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >12</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >13</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >14</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan=1" >
            <Para >15</Para>
        </CELL>
    </ROW>

    <ROW id="r2">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >11</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >12</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >23</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >24</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >15</Para>
        </CELL>
    </ROW>

    <ROW id="r3">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >11</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >12</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >33</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >34</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >15</Para>
        </CELL>
    </ROW>

    <ROW id="r4">
        <CELL rowmerged="F"  rowspan="1" >
            <Para >41</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >42</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >43</Para>
        </CELL>
        <CELL rowmerged="F"  rowspan="1" >
            <Para >44</Para>
        </CELL>
        <CELL rowmerged="T"  rowspan="1" >
            <Para >45</Para>
        </CELL>
    </ROW>
</TBODY>
</TABLE>

规则: 对于合并行:将主合并单元格的内容复制到合并行中的其他单元格。 在这种情况下,你能帮我解决吗? 我是xslt的新手。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您还没有在问题中详细说明规则,但在我看来,如果任何包含rowmerged="T" 的单元格,则可以实现您要求的输出并清空Para,您将Para从最近的前一行中相应的单元格中复制,但不是空的。

您可以像往常一样使用身份模板启动,该模板默认复制所有未更改的内容,但允许您覆盖特定节点的此行为:

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

现在我们要进行更改:对于CELL元素,我们需要修复其属性

  <xsl:template match="CELL">
    <CELL rowmerged="F" rowspan="1">
      <xsl:apply-templates select="node()"/><!-- process children -->
    </CELL>
  </xsl:template>

对于行格式化单元格中的空Para

  <xsl:template match="Para[not(normalize-space())][../@rowmerged='T']">

我们希望找到匹配单元格不为空的最近的前一行,并复制

    <!-- find the number of this cell in the current row -->
    <xsl:variable name="cellnum" select="count(../preceding-sibling::CELL) + 1" />
    <!-- look for the corresponding Para in previous rows -->
    <xsl:variable name="matchingCells" select="
        ../../preceding-sibling::ROW/CELL[$cellnum]/Para" />
    <!-- filter for just the non-empty ones, and copy the nearest (last in doc order) -->
    <xsl:copy-of select="$matchingCells[normalize-space()][last()]" />

完整的样式表如下:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

  <xsl:template match="CELL">
    <CELL rowmerged="F" rowspan="1">
      <xsl:apply-templates select="node()"/>
    </CELL>
  </xsl:template>

  <xsl:template match="Para[not(normalize-space())][../@rowmerged='T']">
    <xsl:variable name="cellnum" select="count(../preceding-sibling::CELL) + 1" />
    <xsl:variable name="matchingCells" select="
        ../../preceding-sibling::ROW/CELL[$cellnum]/Para" />
    <xsl:copy-of select="$matchingCells[normalize-space()][last()]" />
  </xsl:template>
</xsl:stylesheet>