Select Unique

Discussion in 'XML' started by Mystagogue, Dec 11, 2007.

  1. Mystagogue

    Mystagogue Guest

    Given XML like this...

    <stuff>
    <thing id="1" result="true"/>
    <thing id="2" result="true"/>
    <thing id="3" result="false"/>
    <thing id="4" result="false"/>
    <thing id="5" result="error"/>
    </stuff>

    Is there an XPath statement that will select only *unique* "@result"
    samples? For example, given the above, I'd like to have "thing"
    results that consist only of id 1, 3 and 5, because id 2 and 4 are
    duplicate result types. Anybody?
     
    Mystagogue, Dec 11, 2007
    #1
    1. Advertising

  2. Mystagogue

    TOUDIdel Guest

    Uzytkownik "Mystagogue" <> napisal w wiadomosci
    news:...
    > Given XML like this...
    >
    > <stuff>
    > <thing id="1" result="true"/>
    > <thing id="2" result="true"/>
    > <thing id="3" result="false"/>
    > <thing id="4" result="false"/>
    > <thing id="5" result="error"/>
    > </stuff>
    >
    > Is there an XPath statement that will select only *unique* "@result"
    > samples? For example, given the above, I'd like to have "thing"
    > results that consist only of id 1, 3 and 5, because id 2 and 4 are
    > duplicate result types. Anybody?


    In my opinion quite good way is using "attributes features". It does mean in
    one element you may have attributes only with unique names. For example:

    <xsl:apply-templates select=//@*/>
    ....
    <xsl:template match="@*">
    <xsl:attribute name="{.}">whatever</xsl:attribute>
    </xsl:template>

    Result: element with attributes done from unigue (distinct) values

    --
    td
    xmlguru.net
     
    TOUDIdel, Dec 11, 2007
    #2
    1. Advertising

  3. Mystagogue

    Pavel Lepin Guest

    Mystagogue <> wrote in
    <>:
    > <stuff>
    > <thing id="1" result="true"/>
    > <thing id="2" result="true"/>
    > <thing id="3" result="false"/>
    > <thing id="4" result="false"/>
    > <thing id="5" result="error"/>
    > </stuff>
    >
    > Is there an XPath statement that will select only *unique*
    > "@result" samples?


    Not possible with pure XPath1. In XPath2:

    for $a in /stuff/* return
    if (not($a/preceding-sibling::*[@result=$a/@result]))
    then $a
    else /..

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Dec 12, 2007
    #3
  4. Mystagogue wrote:
    > Given XML like this...
    >
    > <stuff>
    > <thing id="1" result="true"/>
    > <thing id="2" result="true"/>
    > <thing id="3" result="false"/>
    > <thing id="4" result="false"/>
    > <thing id="5" result="error"/>
    > </stuff>
    >
    > Is there an XPath statement that will select only *unique* "@result"
    > samples?


    Yes:
    /stuff/thing[not(@result = preceding-sibling::thing/@result)]

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 12, 2007
    #4
  5. Mystagogue

    Pavel Lepin Guest

    Martin Honnen <> wrote in
    <475fccbf$0$17536$-online.net>:
    > Mystagogue wrote:
    >> <stuff>
    >> <thing id="1" result="true"/>
    >> <thing id="2" result="true"/>
    >> <thing id="3" result="false"/>
    >> <thing id="4" result="false"/>
    >> <thing id="5" result="error"/>
    >> </stuff>
    >>
    >> Is there an XPath statement that will select only
    >> *unique* "@result" samples?

    >
    > /stuff/thing[not(@result =
    > preceding-sibling::thing/@result)]


    Well, I'll be damned. It's been a year and a half and I
    still miss clever but essentially simple tricks like that.

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Dec 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kris Rudin
    Replies:
    2
    Views:
    3,885
    Kris Rudin
    Aug 18, 2003
  2. ToshiBoy
    Replies:
    6
    Views:
    877
    ToshiBoy
    Aug 12, 2008
  3. MePadre

    Issue with SELECT TOP 3 / UNIQUE

    MePadre, Dec 11, 2003, in forum: ASP General
    Replies:
    1
    Views:
    111
    Guest
    Dec 15, 2003
  4. palmiere
    Replies:
    1
    Views:
    452
    Erwin Moller
    Feb 9, 2004
  5. Token Type
    Replies:
    9
    Views:
    384
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page