Select Unique

M

Mystagogue

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?
 
T

TOUDIdel

Uzytkownik "Mystagogue said:
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
 
P

Pavel Lepin

Mystagogue said:
<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 /..
 
M

Martin Honnen

Mystagogue said:
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)]
 
P

Pavel Lepin

Martin Honnen said:
Mystagogue said:
<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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top