仅在Tbody中替换表内容的Xslt

时间:2015-01-23 14:03:37

标签: xslt

您可以在下面的方案中帮助我们吗? 我们需要xsl代码用于以下场景。

我们需要在T​​head中的para中检索ref标签 我们需要删除Tbody中para内的ref标签。 对于最后一个单元格,我们不应该执行此ref删除。 ie)应该表现得像thead

样本输入:

<xml>
<Table>
    <thead>
        <Row>
            <Cell>
                <para id=4> 
                    <ref>A</ref>
                </para>
            </Cell>
        </Row>
    </thead>
    <tbody>
        <Row>
            <Cell>
                <para id=1> 
                    <ref>b</ref>
                </para>
            </Cell>
            .
            .
            <Cell>
                <para id=6> 
                    <ref>retrive</ref>
                </para>
            </Cell>
        </Row>
        <Row>
            <Cell>
                <para id=2> 
                    c
                </para>
            </Cell>
            .
            .
            <Cell>
                <para id=7> 
                    <ref>retrive</ref>
                </para>
            </Cell>
        </Row>
        <Row>
            <Cell >
                <para id=3> 
                    <ref>d</ref>
                    <ref>e</ref>
                </para>
            </Cell>
            .
            .
            <Cell>
                <para id=8> 
                    <ref>retrive</ref>
                </para>
            </Cell>
        </Row>
    </tbody>
</table>

预期产出:

   <xml>
  <Table>
    <thead>
        <Row>
            <Cell>
                <para id=4> 
                    <ref>A</ref>   (No change in thead)
                </para>
            </Cell>
        </Row>
    </thead>
    <tbody>
        <Row>
            <Cell>
                <para id=1>   (para attribute should be retrieved)
                    b    (ref tag should be removed but content should be retrieved)
                </para>
            </Cell>
            .
            .
            <Cell>
                <para id=6> 
                    <ref>retrieve</ref>  (Should retrieve ref tag with value)
                </para>
            </Cell>
        </Row>
        <Row>
            <Cell>
                <para id=2> 
                    c
                </para>
            </Cell>
            .
            .
            <Cell>
                <para id=7> 
                    <ref>retrieve</ref>  (Should retrieve ref tag with value)
                </para>
            </Cell>
        </Row>
        <Row>
            <Cell>
                <para id=3> 
                    d
                    e
                </para>
            </Cell>
            .
            .
            <Cell>
                <para id=8> 
                    <ref>retrieve</ref>  (Should retrieve ref tag with value)
                </para>
            </Cell>
        </Row>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

通过调整输入XML以使闭表标记与开始表标记匹配并将id的值包装在引号中,以下XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"  omit-xml-declaration="no"
encoding="UTF-8" indent="yes" />
 <xsl:strip-space elements="*"/>
  <xsl:template match="table">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="tbody/Row/Cell[position()!=last()]/para/ref">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:transform>

当应用于此更正的输入时,XML会生成输出

<?xml version="1.0" encoding="UTF-8"?>
<Table>
  <thead>
    <Row>
     <Cell>
        <para id="4">
           <ref>A</ref>
        </para>
     </Cell>
   </Row>
 </thead>
 <tbody>
  <Row>
     <Cell>
        <para id="1">b</para>
     </Cell>
     <Cell>
        <para id="6">
           <ref>retrieve</ref>
        </para>
     </Cell>
  </Row>
  <Row>
     <Cell>
        <para id="2"> 
                c
            </para>
     </Cell>
     <Cell>
        <para id="7">
           <ref>retrieve</ref>
        </para>
     </Cell>
  </Row>
  <Row>
     <Cell>
        <para id="3">de</para>
     </Cell>
     <Cell>
        <para id="8">
           <ref>retrieve</ref>
        </para>
     </Cell>
    </Row>
  </tbody>
</Table>

<xsl:template match="tbody/Row/Cell[position()!=last()]/para/ref"> 匹配Cell中除最后一个tbody之外的所有position()!=last()元素,并将ref属性替换为其值。