tags:

views:

142

answers:

2

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 verifying that the current node of the template is 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 #)?

+1  A: 

I have worked out the solution myself, a slight change is needed in the xpath adding brackets around the //para limits the selection:

"(//para)[1] intersect ." you can also use "(//para)[1] is ." to get the same result, and on this occassion I belive the "is" version is more readable.

Mike
+2  A: 

In XPath 1.0 (XSLT 1.0) one can use:

count( (//para)[1] | .) = 1

This works also in XPath 2.0, but may not be as efficient as using the intersect operator.

I would recommend the following XPath 2.0 expression:

(//para)[1] is .

Do note the use of the XPath 2.0 is operator.

Dimitre Novatchev
Thanks Dimitre, useful to know the xpath 1.0 version as well. Cheers Mike
Mike