XSL: using xsl:key name to match record

I have the following xml input

<root>
      <Create>
            <Other>
                  <Seat>
                        <SegmentIDRef>2</SegmentIDRef>
                  </Seat>
            </Other>
      <View>
            <Identify>      
                        <Info>
                              <TicketNumber Status="VOIDED">12345</TicketNumber>
                              <SegmentElementNumber TicketNumber="12345">2</SegmentElementNumber>
                        </Info>
                        <Info>
                              <TicketNumber Status="TICKETED">7890</TicketNumber>
                              <SegmentElementNumber TicketNumber="7890">2</SegmentElementNumber>
                        </Info>
                  </Identify>
      </View>
      </Create>
</root>


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']"
                                use="Info/SegmentElementNumber"/>      
      
      <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:call-template>
    </xsl:variable>

      
  <xsl:template name="GetTkt">
    <xsl:param name="Segment" select="''"/>
    <xsl:variable name="TktInfo"     select="key('viewTktMatch', $Segment)" />    
  </xsl:template>
badtz7229Asked:
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"
    version="1.0">


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

Open in new window

0

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:
Gertone:
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.
0
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
0
badtz7229Author Commented:
thanks so much for solution and explanation.
0
Geert BormansInformation ArchitectCommented:
welcome
0
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
CSS

From novice to tech pro — start learning today.