XSL: using xsl:key name to match record

I have the following xml input

                              <TicketNumber Status="VOIDED">12345</TicketNumber>
                              <SegmentElementNumber TicketNumber="12345">2</SegmentElementNumber>
                              <TicketNumber Status="TICKETED">7890</TicketNumber>
                              <SegmentElementNumber TicketNumber="7890">2</SegmentElementNumber>

I want to match the root/Create/Other/Seat/SegmentIdRef with  root/Create/View/Identify/Info/SegmentElementNumber (where @Status  =TICKETED). Then return the @TicketNumber.

So in the above scenario, I want to store in a variable the value: 7890

I have the following code. But do not know how to setup the key in order to match the segmentNumber. Please advise.

  <xsl:key name="TktMatch"  match="/root/Create/View/Identify/Info/TicketNumber[@Status='TICKETED']"
      <xsl:variable name="SeatSegment" select="/root/Create/Other/Seat[position() = 1]/SegmentIDRef"/>
    <xsl:variable name="TktNumber">
      <xsl:call-template name="GetTkt">
        <xsl:with-param name="Segment" select="$SeatSegment"/>

  <xsl:template name="GetTkt">
    <xsl:param name="Segment" select="''"/>
    <xsl:variable name="TktInfo"     select="key('viewTktMatch', $Segment)" />    
Who is Participating?
Geert BormansConnect With a Mentor Information ArchitectCommented:
Here you go

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

            use="." />      
    <xsl:variable name="SeatSegment" select="/root/Create/Other/Seat[position() = 1]/SegmentIDRef"/>
    <xsl:variable name="TktNumber" select="key('TktMatch', $SeatSegment)/@TicketNumber">
    <xsl:template match="/">
        <xsl:value-of select="$TktNumber"/>

Open in new window

badtz7229Author Commented:
thanks that worked.
Can you please explain how you were able to match the records using

xsl:key name= with use="." . What does the use="." do here?

Also, please explain how in xsl:variable name="TktNumber"  you included the attribute alongside the call to key.
Geert BormansConnect With a Mentor Information ArchitectCommented:
Here is what the key definition does
@name : a unique name for this key index
@match : a XPath of a node in the document that would be indexed
@use : the indexer, XPath relative to the context of the node

@match in this key :  View/Identify/Info[TicketNumber/@Status='TICKETED']/SegmentElementNumber
this means we are indexing SegmentElementNumber, but only when they are in the context of an Info element that has a Ticketed status
@use : .
"." means the current node.... relative to SegmentElementNumber... what we will index by is the stringified value of the SegmentElementNumber content

key() function returns all nodes that match a certain index value
key('TktMatch', '2') (first argument is key name, second is index value) will return all these
 <SegmentElementNumber TicketNumber="7890">2</SegmentElementNumber>
as long as they are in a Info element that has such a child
<TicketNumber Status="TICKETED">...

From this
<SegmentElementNumber TicketNumber="7890">2</SegmentElementNumber>
you need the attribute, so
key('TktMatch', $SeatSegment)/@TicketNumber

note: key() returns nodes, so you can use it in an XPath
badtz7229Author Commented:
thanks so much for solution and explanation.
Geert BormansInformation ArchitectCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.