首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT标识转换拆分DITA (CALS)表

使用XSLT标识转换拆分DITA (CALS)表
EN

Stack Overflow用户
提问于 2013-01-24 18:19:56
回答 1查看 430关注 0票数 1

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

输入:

代码语言:javascript
复制
<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>

输出:

代码语言:javascript
复制
<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>

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

EN

回答 1

Stack Overflow用户

发布于 2013-01-24 19:35:39

这样如何:

代码语言:javascript
复制
<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>

给定以下输入:

代码语言:javascript
复制
<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>

生成以下输出:

代码语言:javascript
复制
<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 (因为它有多个根),但我相信它符合您的请求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14498895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档