tags:

views:

568

answers:

1

How can I count how many previous FootNoteReference nodes there are in an xml doc such as this...

<MyDoc>
<title>&lt;Big Title&gt;</title>
<head>Objective</head>
<head2>&lt;Heading&gt;</head2>
<head>Introduction</head>
<Para>
 asdf asdf asdf asd<FootNoteReference />asdf asdf asfd asfd 
</Para>
<head>Overview</head>
<Para>
 &lt;Begin with a definition of the class to which<FootNoteReference /> the categories belong, if required.&gt;
</Para>
<Para>&lt;List the different categories to be taught.&gt;</Para>
<Heading1>&lt;Category 1&gt;</Heading1>
<Para>&lt; som neodolal a celé ozdobené spústou prachu v jednom preso&gt;</Para>
<Para>&lt;Provide examples, if required.&gt;</Para>
<Heading1>&lt;Category 2&gt;</Heading1>
<Para>&lt; som neodolal a celé ozdobené spústou prachu v jednom preso&gt;</Para>
<Para>&lt;Provide examples, if required.&gt;</Para>
<Heading1>&lt;Category 3&gt;</Heading1>
<Para>
 &lt;Provide a description<FootNoteReference /> of the third category as outlined in the list.&gt;
</Para>
<Para>&lt;Provide examples, if required.&gt;</Para>
<head>Summary</head>
<ListItem type="ul">&lt;Summarize the definition, if applicable.&gt;</ListItem>
<ListItem type="ul">&lt; som neodolal a celé ozdobené spústou prachu v jednom preso<FootNoteReference />.&gt;</ListItem>
<ListItem type="ul">&lt; som neodolal a celé ozdobené spústou prachu v jednom preso&gt;</ListItem></MyDoc>

Notice how the FootNoteReference node is nested at different levels. I know if they were all nested at the same level I could do: count(preceding-sibling::*[local-name() = 'FootNoteReference'])

Thanks!

+3  A: 

Use the "preceding" axis:

        count($vNode/preceding::FootNoteReference)

is the number of FootNoteReference elements that precede the node referenced by $vNode.

If the node is a descendent of any FootNoteReference element and you want also to count its ancestor FootNoteReference elements, then the occurences of the FootNoteReference element on the "ancestor" axis should also be accounted for, and this will be accomplished by the following XPath expression:

     count($vNode/preceding::FootNoteReference
             |
               $vNode/ancestor::FootNoteReference)

Dimitre Novatchev
Thanks for the help. This did the trick: select="count(preceding::FootNoteReference)
joe