469,329 Members | 1,470 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Select Unique

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?
Dec 11 '07 #1
4 2550

Uzytkownik "Mystagogue" <ba****@axiscode.comnapisal w wiadomosci
news:72**********************************@18g2000h sf.googlegroups.com...
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
Dec 11 '07 #2

Mystagogue <ba****@axiscode.comwrote in
<72**********************************@18g2000hsf.g ooglegroups.com>:
<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.
Dec 12 '07 #3
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/
Dec 12 '07 #4

Martin Honnen <ma*******@yahoo.dewrote in
<47***********************@newsspool4.arcor-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.
Dec 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Rich | last post: by
9 posts views Thread by Kelvin | last post: by
4 posts views Thread by Kris Rudin | last post: by
2 posts views Thread by Marcel Hug | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.