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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 BormansInformation 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:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.