您可以在下面的方案中帮助我们吗? 我们需要xsl代码用于以下场景。
我们需要在Thead中的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>
答案 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
属性替换为其值。