XPATH:嵌套 select
XPATH: nested select
我们想构建一个 XPath 表达式,为每个 station 选择 transportmittel_detail 节点。
我们目前的做法是这样的:
sbb/station[name="Winterthur"]/transportmittel/../../transportmittel_detail/transportmittel[name=text()]
哪里 sbb/station[name="Winterthur"]/transportmittel
returns:
<transportmittel>S12</transportmittel>
<transportmittel>B1</transportmittel>
<transportmittel>B2</transportmittel>
问题:
我们如何写第二个条件使得text()等于S12,B1,B2.
完整的 XML 看起来像这样:
<?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>
如果您使用 /sbb/transportmittel_detail/transportmittel[name = /sbb/station[name="Winterthur"]/transportmittel]
,那么您 select transportmittel
元素的 name
等于任何 /sbb/station[name="Winterthur"]/transportmittel
元素。
使用 (XPath 1.0):
/sbb/transportmittel_detail
[transportmittel/name = /sbb/station[name='Winterthur']/transportmittel]
或这个 XPath 2.0 表达式:
for $station in /sbb/station[name='Winterthur']
return
/sbb/transportmittel_detail[transportmittel/name = $station/transportmittel]
我们想构建一个 XPath 表达式,为每个 station 选择 transportmittel_detail 节点。
我们目前的做法是这样的:
sbb/station[name="Winterthur"]/transportmittel/../../transportmittel_detail/transportmittel[name=text()]
哪里 sbb/station[name="Winterthur"]/transportmittel
returns:
<transportmittel>S12</transportmittel>
<transportmittel>B1</transportmittel>
<transportmittel>B2</transportmittel>
问题:
我们如何写第二个条件使得text()等于S12,B1,B2.
完整的 XML 看起来像这样:
<?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>
如果您使用 /sbb/transportmittel_detail/transportmittel[name = /sbb/station[name="Winterthur"]/transportmittel]
,那么您 select transportmittel
元素的 name
等于任何 /sbb/station[name="Winterthur"]/transportmittel
元素。
使用 (XPath 1.0):
/sbb/transportmittel_detail
[transportmittel/name = /sbb/station[name='Winterthur']/transportmittel]
或这个 XPath 2.0 表达式:
for $station in /sbb/station[name='Winterthur']
return
/sbb/transportmittel_detail[transportmittel/name = $station/transportmittel]