代码之家  ›  专栏  ›  技术社区  ›  dube

如何在XSL 1.0中跨多个列表获取索引号?

  •  1
  • dube  · 技术社区  · 7 年前

    我需要用XSL 1.0转换xml表。这张桌子有两个街区 thead tbody ,两者都有0。。。n row . 行有条目(又名单元格)。

    <Table>
      <Tgroup cols="6">
        <Thead>
          <Row>
            <Entry> this is row 1 </Entry>
            <Entry> this is row 1</Entry>
          </Row>
        </Thead>
        <Tbody>
          <Row>
            <Entry> this is row 2 </Entry>
            <Entry> this is row 2 </Entry>
          </Row>
          <Row>
            <Entry> this is row 3 </Entry>
            <Entry> this is row 3 </Entry>
          </Row>
        </Tbody>
      </Tgroup>
    </Table>
    

    如何获取行索引(转换时 entry )在两者之上 表格主体 thead公司 ? 当我计算tbody中的行数时,它会忽略thead中的行数。不幸的是,如果有的话,我也得数一数。

    这是我当前的模板,它不符合我的要求:

    <xsl:template match="Entry">
      <!-- only counts inside the same ancestor, e.g. only rows in tbody -->
      <xsl:number count="../../*[name()='Row']" from="*[self::*[name()='Tgroup']]"/>
    </xsl:template>
    

    (是的,我知道 name()='Row' 不是很好,但原始xsl使用变量作为节点名称。)

    1 回复  |  直到 7 年前
        1
  •  1
  •   Tim C    7 年前

    这就是你需要的吗?

     <xsl:number count="*[name()='Row']" level="any"/>
    

    或者,如果您的XML实际上有名称空间(因为我不理解您所说的XSL使用变量作为节点名称是什么意思),那么可能是这样的

     <xsl:number count="*[local-name()='Row']" level="any"/>
    

    不同之处在于后者适用于具有名称空间前缀的情况,如 <my:Row>

    编辑:请尝试使用 from 属性

    <xsl:number count="*[local-name()='Row']" level="any" from="Table"/>