2011-08-04 51 views
1

的前同輩我有這樣的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> 

我要統計所有document.Document當前document.Document前面。 (我不想算video.Video) 例如,如果我在5,我想返回2,而不是4

它看起來像帖子:XSLT - Comparing preceding-sibling's elements with current's node element

我實際上是試圖像(很多東西):

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

感謝的

羅曼

+0

@LarsH:你說得對,我想全名融爲一體。 arsdigita.cms.document.Document),我簡化了XML。感謝你們倆 !! 你的兩個答案很有趣,我會嘗試下一個星期一(我無法測試它),並告訴你最好的一個 – 2011-08-08 08:43:54

回答

0

我不熟悉使用XSL噸,但我用xpath很多。這就產生nav:item元素與document.Document後代的序列:

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

,並從這些nav:item元素之一這個運行,會告訴你類似的元素有多少它的前面:

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

變化preceding-siblingpreceding ,因爲你要計算的屬性不是你試圖從中計算出來的節點的兄弟姐妹(他們有不同的父母)。你也需要做一個完整的條款出來的= 'document.Document'部分:

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

(也許你在想充分'com.arsdigita.cms.document.Document'呢?)

+1

它運作良好!謝謝^^ – Campourcy