xml - Access node based on attribute value -


within xml document, each lead or passenger has pickup , dropoff attribute, has matching waypoint id.

<r:rides>     <r:ride>         <r:lead pickup="1" dropoff="2">         </r:lead>         <r:passengers>             <r:passenger pickup="1" dropoff="3">             </r:passenger>         </r:passengers>         <r:waypoints>             <r:waypoint id="1">                 <a:place>hall</a:place>             </r:waypoint>             <r:waypoint id="2">                 <a:place>apartments</a:place>                    </r:waypoint>             <r:waypoint id="3">                 <a:place>train station</a:place>             </r:waypoint>         </r:waypoints>     </r:ride> </r:rides> 

how select a:place each lead or passenger in xsl? example:

<xsl:for-each select="r:lead">     route: <pickup goes here/> &#8594; <dropoff goes here/>                                      </xsl:for-each> 

expected outcome:

route: hall → apartments

<xsl:for-each select="r:passengers/r:passenger">     route: <pickup goes here/> &#8594; <dropoff goes here/>                                      </xsl:for-each> 

expected outcome:

route: hall → train station

to follow cross-references can define , use key, define key with

<xsl:key name="by-id" match="r:waypoints/r:waypoint/a:place" use="../@id"/> 

then can use e.g.

<xsl:for-each select="r:lead">     route: <xsl:value-of select="key('by-id', @pickup)"/> &#8594; <xsl:value-of select="key('by-id', @dropoff)"/>                                      </xsl:for-each> 

as ids don't seem unique in complete document more code needed, in xslt 2.0 use <xsl:value-of select="key('by-id', @pickup, ancestor::r:ride)"/>.

with xslt 1.0 change key definition

<xsl:key name="by-id" match="r:waypoints/r:waypoint/a:place" use="concat(generate-id(ancestor::r:ride), '|', ../@id)"/> 

and key uses e.g. key('by-id', concat(generate-id(ancestor::r:ride), '|', @pickup)) , on.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -