XSLT: How to find the count of unique children of a node?

后端 未结 2 1855
臣服心动
臣服心动 2021-01-26 17:13

My XML looks like this:


    
        
            
            3&         


        
2条回答
  •  执笔经年
    2021-01-26 18:07

    If all the baz children are same then doSomething else doSomethingElse. My current node is foo.

    This is confusing because:

    • baz are not children of foo;
    • your title says "find the count of unique children" - but it is not necessary to find it in order to know if they are same.

    Try something like:

    
        
        
            
                
            
            
                
            
        
    
    

    Note that this assumes that every baz has a date and a time. Otherwise you need to test for not(date=$first-baz/date) etc.

    See also: http://www.jenitennison.com/xslt/grouping/muenchian.html


    Added:

    Now, assuming all the bar/baz elements have the same tags (not necessarily date and time but say a, b and c), what would be the test attribute for that case?

    This makes it significantly more complex. Still you could construct a key:

    
    

    then make your test:

    
    

    This returns true if any child of baz exists whose string-value is different from an equally named node that is child of the first baz.

    Note that the key, as defined here, is document-wide. If you want to restrict the test to the current foo ancestor, then you must include its id in the key.

提交回复
热议问题