【问题标题】:Splitting DITA (CALS) tables with XSLT - identity transform使用 XSLT 拆分 DITA (CALS) 表 - 身份转换
【发布时间】:2013-01-24 10:19:55
【问题描述】:

我遇到了一个基于 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>

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

【问题讨论】:

  • 你提供了一个例子,但你没有解释它。如果没有进一步的解释,我认为“按列拆分”本身并不意味着任何事情。为什么输出中没有&lt;colspec colnum="2"&gt;?因为不需要?为什么一张桌子变成了两张桌子?确定它的标准是什么?您的目标是制作一系列表格,其中只有一行和该行中的一个条目?如果其中一个源表中有两行应该怎么办?一个 XML 文档中会有多个源表,还是只有一个?
  • 为什么,在源文档中,一行有两个具有相同列名的条目?一行中的单独条目不会属于不同的列吗?
  • 修正了 colnames 的错字,好地方 :)
  • 问题是我必须将 2 列表拆分为两个表,每个表 1 列。我在 Framemaker 11 中遇到了一个错误,我的布局被我的源 XML 的一些内部转换破坏了。该表最初跨越一个边距列,现在 Framemaker 将其销毁,重新调整整个表以适合文本列。为了解决这个问题(现在),我想拆分表格,将侧头放入第一个,将段落放入第二个。
  • 好的,但是输出在两个表中仍然有 colnum 1 和 colname 1。这也是笔误吗?如果有多行,两个表的行数是否应该相同?

标签: xml xslt split html-table dita


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多