0

親の位置に応じてノードを数えようとしています。

これは例です:

<tbody>
    <row>
        <entry>L1C1</entry>
        <entry>L1C2</entry>
        <entry>L1C3</entry>
    </row>
    <row>
        <entry>L2C1</entry>
        <entry morerows="1">L2C2</entry>
        <entry>L2C3</entry>
    </row>
    <row>
        <entry>L3C1</entry>
        <entry>L3C3</entry>
    </row>
</tbody>

各 について、属性が行の位置に依存する数よりも大きい先行要素entryの要素の数を数えたいと思います。entryrowmorerows

私はこのようなものを持っています:

<xsl:variable name="nbRows">
    <xsl:value-of select="count(ancestor::tbody/row)">
    </xsl:value-of>
</xsl:variable>

<xsl:value-of select="count(parent::row/preceding-sibling::row/entry[@morerows &gt; ($nbRows - count(current()/../preceding-sibling::row))])">
</xsl:variable>"/>

しかし、ご想像のとおり、これは機能しません。

誰かがこれで私を助けることができますか?

4

1 に答える 1

2

質問を正しく理解していれば、これでうまくいくはずです:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="row">
    <xsl:variable name="nRows" select="count(../row)"/>
    <xsl:variable name="precedingEntries" select="preceding-sibling::row/entry"/>
    <xsl:variable name="minMoreRows" select="$nRows - position() + 1"/>
    <n>
      <xsl:value-of select="count($precedingEntries[@morerows>=$minMoreRows])"/>
    </n>
  </xsl:template>

  <xsl:template match="/">
    <root>
      <xsl:apply-templates/>
    </root>
  </xsl:template>

</xsl:stylesheet>

質問の例に適用した場合の出力は次のとおりです。

<root>
    <n>0</n>
    <n>0</n>
    <n>1</n>
</root>
于 2013-05-06T15:24:16.960 に答える