Friday, February 11, 2011

Xslt - Enforcing @attribute uniqueness.

I rarely work with xslt's so I'm not the greatest at it, but, I was wondering how to go about solving this problem:

<Element>
   <childElement type="type1">Bob</childElement>
   <childElement type="type1">Smith</childElement>
   <childElement type="type2">Bob</childElement>
</Element>"

I want to enforce @type uniqueness, where I would just grab one of the ChildElements if they have the same @type. Different @types can have the same value (in this example, the 2 Bobs would be okay). Any help on how to solve this in xslt?

  • Here is an XPath to grab only the unique @type values...

    //childElement[not(@type=preceding::childElement/@type)]
    

    ...result from your example is...

    Location: 3:5
    Description: /Element[1]/childElement[1] - Bob
    Location: 5:5
    Description: /Element[1]/childElement[3] - Bob
    
    Cochese : Brilliant! I've never used xpath before, just very basic xslt transformations. Thanks a bunch for answer!
    From dacracot

0 comments:

Post a Comment