RE: [xsl] selecting a section element which parent element doesn't have any element other than title element

Subject: RE: [xsl] selecting a section element which parent element doesn't have any element other than title element
From: <srinivasan.p@xxxxxxxxxx>
Date: Thu, 6 Aug 2009 15:21:12 +0530
Thanks Michael. It's working fine for me.

Thanks
Srinivasan

-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
Sent: Thursday, August 06, 2009 3:05 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] selecting a section element which parent element doesn't
have any element other than title element



Something like this:

<!-- Rule for a Type 1 section -->

<xsl:template match="section">
  <xsl:copy>
    <xsl:attribute name="role">
      <xsl:text>H</xsl:text>
      <xsl:value-of select="count(ancestor::section)+1"/>
    </xsl:attribute>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<!-- Rule for a Type 2 section -->

<xsl:template match="section[not(preceding-sibling::*[not(self::title)]]">
  <xsl:copy>
    <xsl:attribute name="role">
      <xsl:for-each select="reverse(1 to count(ancestor::section)+1)"/>
        <xsl:text>H</xsl:text>
        <xsl:value-of select="."/>
      </xsl:for-each>
    </xsl:attribute>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay

> -----Original Message-----
> From: srinivasan.p@xxxxxxxxxx [mailto:srinivasan.p@xxxxxxxxxx]
> Sent: 06 August 2009 10:21
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] selecting a section element which parent
> element doesn't have any element other than title element
>
> Hi Team,
>
> I need the following output from the following input. In the following
> Input1 example, "..." means any opening and closing tag can
> be present.
>
> Input1:
>
> <book>
> <section>
> <title>Section 1 title Present here</title> ...
> <section>
> <title>Section 2 title present here</title> </section>
> </section> </book>
>
> Output1:
>
> <book>
> <section role="H1">
> <title>Section 1 title Present here</title> ...
> <section role="H2">
> <title>Section 2 title present here</title> </section>
> </section> </book>
>
>
> Input2:
>
> <book>
> <section>
> <title>Section 1 title Present here</title> <section>
> <title>Section 2 title present here</title> </section>
> </section> </book>
>
> Output2:
>
> <book>
> <section role="H1">
> <title>Section 1 title Present here</title> <section
> role="H2H1"> <title>Section 2 title present here</title>
> </section> </section> </book>           
>
> I am using the same style sheet to achieve these two outputs.
> I dont have any issues in achieving output1. The problem is
> in output2. I need to select the "section" element which
> parent is "section" that doesn't contain any tags other than
> "title" element.
>
> Can you please anyone suggest to me how to achieve this in XSLT 2.0?
>
> Thanks in advance.
>
> Thanks
> Srinivasan
>  

Current Thread