xpath 2.0 query - how to test for the first occurence of an element in a xml document

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-03-23T09:16:47Z Indexed on 2010/03/23 11:23 UTC
Read the original article Hit count: 342

Filed under:
|
|

I have the following xsl template:

<xsl:template match="para">
    <fo:block xsl:use-attribute-sets="paragraph.para">
        <!-- if first para in document -->
        <!--<xsl:if test="//para[1] intersect .">-->
        <xsl:if test="//para[1] intersect .">
            <xsl:attribute name="space-after">10pt</xsl:attribute>
            <xsl:attribute name="background-color">yellow</xsl:attribute>
        </xsl:if> 
        <xsl:choose>
            <xsl:when test="preceding-sibling::*[1][self::title]">
                <xsl:attribute name="text-indent">0em</xsl:attribute>
            </xsl:when>
            <xsl:when test="parent::item">
                <xsl:attribute name="text-indent">0em</xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="text-indent">1em</xsl:attribute>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:apply-templates/>
    </fo:block>
</xsl:template>

The problem I am having is selecting the very first para node in the document from the following xml:

<document>
    <section>
        <paragraph>
            <para>Para Text 1*#</para>
            <para>Para Text 2</para>
        </paragraph>
    </section>
    <paragraph>
        <para>Para Text 3*</para>
        <para>Para Text 4</para>
        <para>Para Text 5</para>
        <sub-paragraph>
            <para>Para Text 6*</para>
            <para>Para Text 7</para>
        </sub-paragraph>
    </paragraph>
    <appendix>
        <paragraph>
            <para>Para Text 8*</para>
        </paragraph>
        <paragraph>
            <para>Para Text 9</para>
        </paragraph>        
    </appendix>
</document>

the xpath I am currently using is "//para[1] intersect ." which is selecting the first para for each group of para (denoted with a * in XML sample). Any ideas on how I can just select the first occurance of para within document (denoted with a #)?

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xpath