1

我有一个基于 Framemaker 错误的主要问题,我试图为此构建一个解决方法。在一个文档中,我们有很多表,我必须按列拆分。这些表在元素中包含一个属性,可以用来识别它们。所以这就是我需要的:

输入:

<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>

我快要放弃了,因为到目前为止我所尝试的一切都没有成功:(

4

1 回答 1

1

这个怎么样:

<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(因为它有多个根),但我相信它符合您的要求。

于 2013-01-24T11:35:38.767 回答