我有一个基于框架生成器错误的主要问题,我试图为它建立一个解决方法。在一个文档中,我们有很多表,我必须按列拆分。这些表在元素中包含一个属性,可以用来标识这些表。所以这是我需要的:
输入:
<table attributes*>
<tgroup attributes* outputclass="identifier">
<colspec colnum="1" colname="1" attributes*/>
<colspec colnum="2" colname="2" attributes*/>
<tbody>
<row attributes*>
<entry colname="1">sometext</entry>
<entry colname="2">moretext</entry>
</row>
</tbody>
</tgroup>
</table>输出:
<table attributes*>
<tgroup attributes* outputclass="identifier1">
<colspec colnum="1" colname="1" attributes*/>
<tbody>
<row attributes*>
<entry colname="1">sometext</entry>
</row>
</tbody>
</tgroup>
</table>
<table attributes*>
<tgroup attributes* outputclass="identifier2">
<colspec colnum="1" colname="1" attributes*/>
<tbody>
<row attributes*>
<entry colname="1">moretext</entry>
</row>
</tbody>
</tgroup>
</table>我快要放弃了,因为到目前为止我尝试过的一切都没有成功:
发布于 2013-01-24 19:35:39
这样如何:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="text()" />
<xsl:template match="colspec">
<xsl:apply-templates select="../.." mode="copyTable">
<xsl:with-param name="colSpec" select="." />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@* | node()" mode="copyTable">
<xsl:param name="colSpec" />
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="copyTable">
<xsl:with-param name="colSpec" select="$colSpec" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="tgroup/@outputclass" mode="copyTable">
<xsl:param name="colSpec" />
<xsl:attribute name="{name()}">
<xsl:value-of select="concat(., $colSpec/@colname)" />
</xsl:attribute>
</xsl:template>
<xsl:template match="colspec" mode="copyTable">
<xsl:param name="colSpec" />
<xsl:if test="generate-id() = generate-id($colSpec)">
<xsl:copy>
<xsl:apply-templates select="@*" mode="copyTable">
<xsl:with-param name="colSpec" select="$colSpec" />
</xsl:apply-templates>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="@colnum | @colname" mode="copyTable">
<xsl:attribute name="{name()}">
<xsl:text>1</xsl:text>
</xsl:attribute>
</xsl:template>
<xsl:template match="row" mode="copyTable">
<xsl:param name="colSpec" />
<xsl:copy>
<xsl:apply-templates select="@*" mode="copyTable"/>
<xsl:apply-templates select="entry[@colname = $colSpec/@colname]"
mode="copyTable"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>给定以下输入:
<table a="1" b="2">
<tgroup c="3" d="4" outputclass="identifier">
<colspec colnum="1" colname="1" e="5" f="2"/>
<colspec colnum="2" colname="2" e="2" f="1"/>
<tbody>
<row g="1" h="2">
<entry colname="1">sometext</entry>
<entry colname="2">moretext</entry>
</row>
<row g="1" h="2">
<entry colname="1">somemoretext</entry>
<entry colname="2">moremoretext</entry>
</row>
</tbody>
</tgroup>
</table>生成以下输出:
<table a="1" b="2">
<tgroup c="3" d="4" outputclass="identifier1">
<colspec colnum="1" colname="1" e="5" f="2" />
<tbody>
<row g="1" h="2"><entry colname="1">sometext</entry></row>
<row g="1" h="2"><entry colname="1">somemoretext</entry></row>
</tbody>
</tgroup>
</table>
<table a="1" b="2">
<tgroup c="3" d="4" outputclass="identifier2">
<colspec colnum="1" colname="1" e="2" f="1" />
<tbody>
<row g="1" h="2"><entry colname="1">moretext</entry></row>
<row g="1" h="2"><entry colname="1">moremoretext</entry></row>
</tbody>
</tgroup>
</table>实际上,它不是有效的XML (因为它有多个根),但我相信它符合您的请求。
https://stackoverflow.com/questions/14498895
复制相似问题