xslt - 通过 XSL 添加行跨度

标签 xslt

我有以下 xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<Loci>
    <Locus>
        <Id>MTBC1726</Id>
        <Alleles>
            <Allele>
                <Name>1.0</Name>
                <Description>No annotation provided</Description>
            </Allele>
            <Allele>
                <Name>2.0</Name>
                <Description>No annotation provided</Description>
            </Allele>
        </Alleles>
    </Locus>
    <Locus>
        <Id>MTBC3142</Id>
        <Alleles>
            <Allele>
                <Name>1.0</Name>
                <Description>No annotation provided</Description>
            </Allele>
        </Alleles>
    </Locus>
</Loci>

我想创建以下结果:

enter image description here

在 HTML 中,它看起来像这样:

<html>
<body>

<table border="1">
  <tr>
    <th>Locus</th>
    <th>Allele</th>
    <th>Description</th>
  </tr>
  <tr>
    <td rowspan="2">MTBC1726</td>
    <td>1.0</td>
    <td >No annotation provided</td>
  </tr>
  <tr>
    <td>2.0</td>
    <td >No annotation provided</td>
  </tr>
  <tr>
    <td >MTBC3142</td>
    <td>1.0</td>
    <td >No annotation provided</td>
  </tr>
</table>

</body>
</html>

我创建了以下 XSL:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <table border="1">
      <tr>
        <th>Locus</th>
        <th>Allele</th>
        <th>Description</th>
      </tr>
      <xsl:for-each select="Loci/Locus">
      <tr>
        <td><xsl:value-of select="Id"/></td>
        <xsl:for-each select="Alleles/Allele">
          <td><xsl:value-of select="Name"/></td>
          <td><xsl:value-of select="Description"/></td>
        </xsl:for-each>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

但这会产生这样的表格:

enter image description here

所以,我的问题是:如何添加 rowspan具有基于 <Allele> 数量的计数的属性节点?

最佳答案

这个怎么样:

<table border="1">
  <tr>
    <th>Locus</th>
    <th>Allele</th>
    <th>Description</th>
  </tr>
  <xsl:for-each select="Loci/Locus">
    <xsl:for-each select="Alleles/Allele">
      <tr>
        <xsl:if test="position() = 1">
          <td rowspan="{last()}">
            <xsl:value-of select="ancestor::Locus[1]/Id"/>
          </td>
        </xsl:if>
        <td><xsl:value-of select="Name"/></td>
        <td><xsl:value-of select="Description"/></td>
      </tr>
    </xsl:for-each>
  </xsl:for-each>
</table>

这里的想法是,在内部 for-each 中,您可以使用 position() 来查看您是否在第一个 Allele 对于此 Locuslast() 给出当前 Locus 包含的 Allele 元素的数量。 ancestor::Locus[1] 是因为我们需要在当前上下文是其第一个 Allele 的点提取 Locus Id。

如果您想避免为单等位基因位点添加 rowspan="1",则必须使用条件 xsl:attribute 而不是属性值模板:

        <xsl:if test="position() = 1">
          <td>
            <xsl:if test="last() &gt; 1">
              <xsl:attribute name="rowspan">
                <xsl:value-of select="last()" />
              </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="ancestor::Locus[1]/Id"/>
          </td>
        </xsl:if>

关于xslt - 通过 XSL 添加行跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19684198/

相关文章:

C# 与 XSLT 将 XML 数据转换为 html 文件

xml - 根据属性值修改元素的值

xml - 使用 XSLT <xsl :when>? 时如何指定 "equals to”

XSLt 空白命名空间

xml - 如何使用 XSL 复制子项

xml - 获取 XML 中的属性值

xml - XSLT:如何使用 XSLT 将 xml 数据转换为层次结构

xslt - 学习 XSLT 和 "Real"函数式编程的技巧和陷阱?

c# - 从 XSL :FO using C# 生成 HTML

xml - 在 Excel 上显示时,邮政编码会去掉前导零。导出到 excel 从 vb.net XML 到 XSLT 转换