user3354853
user3354853

Reputation: 187

how to find the find the following scenario in XSLT

I have content like this:

<sect1>
    <title>Leiblichkeit und Verk&#x00F6;rperung</title>
    <sect2>
        <title>Leiblichkeit und Verk&#x00F6;rperung</title>
        <para>
        .......
        </para>
    </sect2>
    <sect2>
        <title>Leiblichkeit und Verk&#x00F6;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&#x00F6;rperung</title>
    <sect2>
        <title aid:pstyle="below_2_head">Leiblichkeit und Verk&#x00F6;rperung</title>
        <para>
        .......
        </para>
    </sect2>
    <sect2>
        <title aid:pstyle="2_head">Leiblichkeit und Verk&#x00F6;rperung</title>
        <para>
        .......
        </para>
    </sect2>
</sect1>

Upvotes: 1

Views: 27

Answers (1)

Ian Roberts
Ian Roberts

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

Related Questions