Campourcy
Campourcy

Reputation: 11

preceding-sibling of current text node

I have this kind of XML :

<nav:objectList>
    <nav:item >
      <nav:attribute name="display">1</nav:attribute> 
      <nav:attribute name="className">document.Document</nav:attribute> 
      <nav:attribute name="title">item 1</nav:attribute> 
      <nav:attribute name="getFileExtension">pdf</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">2</nav:attribute> 
      <nav:attribute name="className">video.Video</nav:attribute> 
      <nav:attribute name="title">item 2</nav:attribute> 
      <nav:attribute name="getFileExtension">mp4</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">3</nav:attribute> 
      <nav:attribute name="className">document.Document</nav:attribute> 
      <nav:attribute name="title">item 3</nav:attribute> 
      <nav:attribute name="getFileExtension">pdf</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">4</nav:attribute> 
      <nav:attribute name="className">video.Video</nav:attribute> 
      <nav:attribute name="title">item 4</nav:attribute> 
      <nav:attribute name="getFileExtension">mp4</nav:attribute> 
    </nav:item>
    <nav:item >
      <nav:attribute name="display">5</nav:attribute> 
      <nav:attribute name="className">document.Document</nav:attribute> 
      <nav:attribute name="title">item 5</nav:attribute> 
      <nav:attribute name="getFileExtension">pdf</nav:attribute> 
    </nav:item>
</nav:objectList>

I want to count all document.Document preceding the current document.Document. (I don't want to count the video.Video) For example, if i am on the 5, I want to return 2 and not 4.

It looks like the post : XSLT - Comparing preceding-sibling's elements with current's node element

I was actually trying (many things) like :

count(preceding-sibling::nav:attribute[@name='type.className']='com.arsdigita.cms.document.Document'

Thank's

Romain

Upvotes: 1

Views: 896

Answers (2)

LarsH
LarsH

Reputation: 28004

Change preceding-sibling to preceding, because the attributes you're trying to count are not siblings of the node you're trying to count from (they have different parents). Also you need to make a complete clause out of the = 'document.Document' part:

count(preceding::nav:attribute[@name='type.className'
                               and . = 'document.Document'])

(or maybe you want the full 'com.arsdigita.cms.document.Document' in there?)

Upvotes: 1

MattH
MattH

Reputation: 38255

I'm not familiar with xslt, but I use xpath plenty. This yields a sequence of nav:item elements with the document.Document descendents:

//nav:item[nav:attribute[@name="className" and text()="document.Document"]]

And this run from one of those nav:item elements, will tell you how many similar elements precede it:

count(preceding-sibling::nav:item[nav:attribute[@name="className" and text()="document.Document"]])

Upvotes: 0

Related Questions