1

I want to count the parent elements and return count in String format. I have mentioned tried code below.

Input:

<node>
    <dot>
        <title>paragaraph 12</title>
    </dot>
    <dot>
        <dot>
            <dot>
                <ttle>paragaraph 23</ttle>
            </dot>
        </dot>
    </dot>
    <dot>
        <dot>
            <title>paragaraph 24</title>
        </dot>
    </dot>
</node>

Tried code:

<xsl:template match="title[parent::dot]">
    <xsl:value-of select="count(parent::dot)"/>
</xsl:template>

Expected output:

<output>
    <out type="One">paragaraph 12</out>
    <out type="Three">paragaraph 23</out>
    <out type="Two">paragaraph 24</out>
</output>

Explain output:

<out type="One">paragaraph 12</out> --> here there are one <dot> above title. so @type should be as One

<out type="One">paragaraph 23</out> --> here there are three <dot> above title. so @type should be as Three

<out type="One">paragaraph 24</out> --> here there are two <dot> above title. so @type should be as Two

user_v12
  • 559
  • 5
  • 15

2 Answers2

2

xsl:number with format="Ww" should produce the right output:

  <xsl:template match="dot/title">
      <out>
          <xsl:attribute name="count">
              <xsl:number format="Ww" value="count(ancestor::dot)"/>
          </xsl:attribute>
          <xsl:apply-templates/>
      </out>
  </xsl:template>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

What you're looking for is the ancestor-axis:

  <xsl:template match="title[parent::dot]">
    <xsl:element name="out">
      <xsl:attribute name="type" select="count(ancestor::dot)"/>
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

To convert the numbers to words check out this question: Convert Number into english Word-Meaning using XSLT

user_v12
  • 559
  • 5
  • 15
iddo
  • 749
  • 3
  • 11