Reputation: 187
I have content like this:
<sect1>
<title>Leiblichkeit und Verkörperung</title>
<sect2>
<title>Leiblichkeit und Verkörperung</title>
<para>
.......
</para>
</sect2>
<sect2>
<title>Leiblichkeit und Verkörperung</title>
<para>
.......
</para>
</sect2>
</sect1>
I want to match and apply attribute for sect2
's title
if its continues to sect1
as "below_2_head"
I tried:
<xsl:template match="title">
<xsl:copy>
<xsl:if test="parent::sect1"><xsl:attribute name="aid:pstyle">1_head</xsl:attribute></xsl:if>
<xsl:if test="parent::sect2"><xsl:attribute name="aid:pstyle"><xsl:if test="parent::sect2/preceding-sibling::title">below_</xsl:if>2_head</xsl:attribute></xsl:if>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
I want the output as:
<sect1>
<title aid:pstyle="1_head">Leiblichkeit und Verkörperung</title>
<sect2>
<title aid:pstyle="below_2_head">Leiblichkeit und Verkörperung</title>
<para>
.......
</para>
</sect2>
<sect2>
<title aid:pstyle="2_head">Leiblichkeit und Verkörperung</title>
<para>
.......
</para>
</sect2>
</sect1>
Upvotes: 1
Views: 27
Reputation: 122374
I suspect the problem is here:
<xsl:if test="parent::sect2/preceding-sibling::title">
This will be true for both of the sect2
elements in your example document as they both have some preceding sibling element that is a title
. What you actually need to check is just the immediately preceding sibling element, to see whether that element is a title or not:
<xsl:if test="parent::sect2/preceding-sibling::*[1][self::title]">
Upvotes: 2