0

I have a problem with XPATH. I have an XML structred like this:

<root>
    <state name="UsUed">
        <transition event-source="this" event="edit()" target="UsEd_editing" />
        ...
    </state>
    <state>
        <transition event-source="this" event="edit()" target="UsEd" />
        <transition event-source="that" event="new()" target="SUed" />
        ...
    </state>
</root>

and I need to get <transition />'s only with @event-source='this' and distinct @event.

My solution to this point is selecting all <transitions /> with 'this' attribute, sorts them by @event then tries to select only distinct ones of them like here

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:for-each select="//transition[@event-source='this']">
        <xsl:sort select="@event"/>
            <xsl:if https://stackoverflow.com/questions/2812122/distinct-in-xpath>
                <!--Here goes the transformation-->
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

But it doesn't work.

Community
  • 1
  • 1
ASMik09
  • 33
  • 5

1 Answers1

0

For selecting unique items, you can use the preceding axis to compare the current match to previous matches to make sure they aren't the same. An XPath query like the following should be all you need to select the unique items. I made the assumption that the uniqueness of the @event attribute was limited to other transition elements with the @event-source='this' and not global.

/root/state/transition[@event-source='this'][not(@event=preceding::transition[@event-source='this']/@event)]
AlwaysWrong
  • 526
  • 4
  • 9