0

我对 XPATH 有疑问。我有一个这样的 XML 结构:

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

我只需要得到<transition />'s only with@event-source='this'和 distinct @event

我对这一点的解决方案是选择所有<transitions />具有“this”属性的,然后对它们进行排序,@event然后尝试只选择其中不同的,比如这里

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

但它不起作用。

4

1 回答 1

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)]
于 2013-01-15T21:02:35.440 回答