Search code examples
xmlxpathxpath-2.0domxpath

XPATH: nested select


We would like to build an XPath expression that selects the transportmittel_detail node for each station.

Our current approach looks like this:

sbb/station[name="Winterthur"]/transportmittel/../../transportmittel_detail/transportmittel[name=text()]

where sbb/station[name="Winterthur"]/transportmittel returns:

<transportmittel>S12</transportmittel> <transportmittel>B1</transportmittel> <transportmittel>B2</transportmittel>

Question:

How do we write the second condition so that text() is equals to S12, B1, B2.

The full XML looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<sbb>
    <station>
        <name>Zürich, Schwamendingerplatz</name>
        <coordinates>
            <lat>47.4042</lat>
            <long>8.5739</long>
            <height>434</height>
        </coordinates>
        <transportmittel>T7</transportmittel>
        <transportmittel>T9</transportmittel>
    </station>
    <station>
        <name>Winterthur</name>
        <coordinates>
            <lat>47.5003</lat>
            <long>47.5003</long>
            <height>466</height>
        </coordinates>
        <transportmittel>S12</transportmittel>
        <transportmittel>B1</transportmittel>
        <transportmittel>B2</transportmittel>
    </station>
    <station>
        <name>Stettbach, Bahnhof</name>
        <coordinates>
            <lat>47.3972</lat>
            <long>8.5961</long>
            <height>437</height>
        </coordinates>
        <transportmittel>S12</transportmittel>
        <transportmittel>T7</transportmittel>
        <transportmittel>T12</transportmittel>
        <transportmittel>B760</transportmittel>
    </station>
    <schedule>
        <connection>
            <from>Zürich, Schwamendingerplatz</from>
            <to>Stettbach, Bahnhof</to>
            <departure>08:43</departure>
            <arrival>08:48</arrival>
            <transportmittel>T7</transportmittel>
        </connection>
        <connection>
            <from>Stettbach, Bahnhof</from>
            <to>Winterthur</to>
            <departure>08:56</departure>
            <arrival>09:09</arrival>
            <track_departure>2</track_departure>
            <track_arrival>5</track_arrival>
            <transportmittel>S12</transportmittel>
        </connection>
    </schedule>
    <transportmittel_detail>
        <transportmittel>
            <name>T7</name>
            <departure>Zürich, Wollishofen</departure>
            <arrival>Stettbach, Bahnhof</arrival>
        </transportmittel>
        <transportmittel>
            <name>S12</name>
            <departure>Brugg AG</departure>
            <arrival>Winterthur, Seen</arrival>
        </transportmittel>
    </transportmittel_detail>
</sbb>

Solution

  • Use (XPath 1.0):

    /sbb/transportmittel_detail [transportmittel/name = /sbb/station[name='Winterthur']/transportmittel]

    or this XPath 2.0 expression:

    for $station in /sbb/station[name='Winterthur'] return /sbb/transportmittel_detail[transportmittel/name = $station/transportmittel]