XSL: find matching Traveler by position in XML (since no unique identifier is found)

I have the following xsl and input file.
I want to populate Loyal/@givenName & Loyal/@surname by matching the TravelerID with the POSITION of where TravelerName exists.

Example:

TravelerIDs[@ID=T1]/Info/FFNumber  is linked to the 2nd Traveler in Request/TravelerName (i.e - JOHN DOE )
The way the TravelerName's are ordered is the same as the way the TravelerIds are odered



input file
<root>
	<Request>
		<TravelerName>
			<Surname>DOE</Surname>
			<GivenName>JANE</GivenName>
		</TravelerName>
		<TravelerName>
			<Surname>DOE</Surname>
			<GivenName>JOHN</GivenName>
		</TravelerName>
		<TravelerIDs ID="T2" Type="ADT"/>
		<TravelerIDs ID="T1" Type="ADT">
			<Info>
				<FFNumber>1234</FFNumber>
				<FFCompanyCode>RT</FFCompanyCode>
			</Info>
		</TravelerIDs>
	</Request>
</root>

Open in new window




xsl code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/>

  <xsl:template match="/root">
    <Request>
      <xsl:for-each select="Request/TravelerIDs">
        <Qualifiers>
          <xsl:attribute name="Type">
            <xsl:value-of select="@Type"/>
          </xsl:attribute>
          <Traveler>
            <xsl:value-of select="translate ( @ID, 'T', '')"/>
          </Traveler>
          <xsl:if test="Info">
            <Loyal>
              <xsl:attribute name="code">
                <xsl:value-of select="Info/FFCompanyCode"/>
              </xsl:attribute>
              <xsl:attribute name="givenName">
                <xsl:value-of select="../Traveler/TravelerName/GivenName"/>
              </xsl:attribute>
              <xsl:attribute name="surname">
                <xsl:value-of select="../Traveler/TravelerName/Surname"/>
              </xsl:attribute>
              <xsl:value-of select="FQTVInfo/FFNumber"/>
            </Loyal>
          </xsl:if>
        </Qualifiers>
      </xsl:for-each>
    </Request>
  </xsl:template>
</xsl:stylesheet>

Open in new window

badtz7229Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Gertone (Geert Bormans)Information ArchitectCommented:
Try this for positional reference

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/>
    
    <xsl:template match="/root">
        <Request>
            <xsl:for-each select="Request/TravelerIDs">
                <xsl:variable name="position" select="position() - 1"></xsl:variable>
                <Qualifiers>
                    <xsl:attribute name="Type">
                        <xsl:value-of select="@Type"/>
                    </xsl:attribute>
                    <Traveler>
                        <xsl:value-of select="translate ( @ID, 'T', '')"/>
                    </Traveler>
                    <xsl:if test="Info">
                        <Loyal>
                            <xsl:attribute name="code">
                                <xsl:value-of select="Info/FFCompanyCode"/>
                            </xsl:attribute>
                            <xsl:attribute name="givenName">
                                <xsl:value-of select="../TravelerName[count(preceding-sibling::TravelerName) = $position]/GivenName"/>
                            </xsl:attribute>
                            <xsl:attribute name="surname">
                                <xsl:value-of select="../TravelerName[count(preceding-sibling::TravelerName) = $position]/Surname"/>
                            </xsl:attribute>
                            <xsl:value-of select="FQTVInfo/FFNumber"/>
                        </Loyal>
                    </xsl:if>
                </Qualifiers>
            </xsl:for-each>
        </Request>
    </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:
that worked

can you please explain how you are doing this?
0
Gertone (Geert Bormans)Information ArchitectCommented:
this variable
                <xsl:variable name="position" select="position() - 1"></xsl:variable>
counts the number of TravelerIDs before the current TravelerIDs

I then use that variable in this expression
../TravelerName[count(preceding-sibling::TravelerName) = $position]
is the TravelerName that has a number of preceding sibling TravelerName elements equal to $position
in other words: it returns the TravelerName at the exact relative position as the TravelerIds we are currently processing
0
badtz7229Author Commented:
Thank u
0
Gertone (Geert Bormans)Information 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
XML

From novice to tech pro — start learning today.