XSLT: Use values in multiple locations

I'm working on a section of an xslt transform that's giving me trouble. The input data looks like this:
<AnswerSet>
       <Answer name="ApplicantAddressLine1">
		<RptValue>
			<TextValue>1 Oak St.</TextValue>
			<TextValue>4 Spring St.</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ApplicantAddressType">
		<RptValue>
			<TextValue>Current</TextValue>
			<TextValue>Previous</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ApplicantCity">
		<RptValue>
			<TextValue>Portland</TextValue>
			<TextValue>Portland</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ApplicantCityID">
		<RptValue>
			<TextValue>9F159DD3420</TextValue>
			<TextValue>9DS3E87FEI0</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ApplicantCounty">
		<RptValue>
			<TextValue>Cumberland</TextValue>
			<TextValue>Cumberland</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ApplicantState">
		<RptValue>
			<TextValue>ME</TextValue>
			<TextValue>ME</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ApplicantZip">
		<RptValue>
			<TextValue>04102</TextValue>
			<TextValue>04101</TextValue>
		</RptValue>
	</Answer>
        <Answer name="ChildFirstName">
		<RptValue>
			<TextValue>Chillie</TextValue>
			<TextValue>Chester</TextValue>
			<TextValue>Conchita</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ChildFullName">
		<RptValue>
			<TextValue>Chillie C Cheetah</TextValue>
			<TextValue>Chester C Cheetah Jr.</TextValue>
			<TextValue>Conchita C Cheetah</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ChildLastName">
		<RptValue>
			<TextValue>Cheetah</TextValue>
			<TextValue>Cheetah</TextValue>
			<TextValue>Cheetah</TextValue>
		</RptValue>
	</Answer>
	<Answer name="ChildMiddleInitial">
		<RptValue>
			<TextValue>C</TextValue>
			<TextValue>C</TextValue>
			<TextValue>C</TextValue>
		</RptValue>
	</Answer>
        <Answer name="ChildDOB">
		<RptValue>
			<DateValue>1/11/2003</DateValue>
			<DateValue>27/6/2000</DateValue>
			<DateValue>9/10/1998</DateValue>
		</RptValue>
	</Answer>
</AnswerSet

Open in new window

I want the output to look like this:
<Children>
    <Child>
      <ChildFirstName>Chillie</ChildFirstName>
      <ChildMiddleInit>R</ChildMiddleInit>
      <ChildLastName>Cheetah</ChildLastName>
      <ChildSuffix/>
      <ChildDOB>11/1/2003</ChildDOB>     
      <ChildAddress1>1 Oak St.</ChildAddress1>
      <ChildCity>Portland</ChildCity>
      <ChildState>ME</ChildState>
      <ChildZipCode>04102</ChildZipCode>
      <ChildCounty>Cumberland</ChildCounty>
      <ChildAddressType>Current</ChildAddressType>
      <ChildCityID>9F159DD342</ChildCityID>
    </Child>
  </Children>
  <Children>
    <Child>
      <ChildFirstName>Chester</ChildFirstName>
      <ChildMiddleInit>C</ChildMiddleInit>
      <ChildLastName>Cheetah</ChildLastName>
      <ChildSuffix>Jr.</ChildSuffix>
      <ChildDOB>6/27/2000</ChildDOB>      
      <ChildAddress1>1 Oak St.</ChildAddress1>
      <ChildCity>Portland</ChildCity>
      <ChildState>ME</ChildState>
      <ChildZipCode>04102</ChildZipCode>
      <ChildCounty>Cumberland</ChildCounty>
      <ChildAddressType>Current</ChildAddressType>
      <ChildCityID>9F159DD3420</ChildCityID>
    </Child>
  </Children>
  <Children>
    <Child>
      <ChildFirstName>Conchita</ChildFirstName>
      <ChildMiddleInit>C</ChildMiddleInit>
      <ChildLastName>Cheetah</ChildLastName>
      <ChildSuffix/>
      <ChildDOB>10/9/1998</ChildDOB>      
      <ChildAddress1>1 Oak St.</ChildAddress1>
      <ChildCity>Portland</ChildCity>
      <ChildState>ME</ChildState>
      <ChildZipCode>04102</ChildZipCode>
      <ChildCounty>Cumberland</ChildCounty>
      <ChildAddressType>Current</ChildAddressType>
      <ChildCityID>9F159DD3420</ChildCityID>
    </Child>
  </Children>

Open in new window

Basically, I want the applicant's current address to be inserted as each child's address. I'm stumped as to how to do it. Here's is my latest attempt:
<xsl:template match="//Answer[@name='ChildFirstName']/RptValue/TextValue">

	<xsl:variable name="getchildren" select="position()"/>	
	<xsl:variable name="childfullname" select="//Answer[@name='ChildFullName']//TextValue[$getchildren]"/>
	<xsl:variable name="childmiddleinit" select="//Answer[@name='ChildMiddleInitial']//TextValue[$getchildren]"/>
	<xsl:variable name="childlastname" select="//Answer[@name='ChildLastName']//TextValue[$getchildren]"/>
	<xsl:variable name="childsuffix" select="//Answer[@name='ChildNameSuffix']//MCValue[$getchildren]"/>
	<xsl:variable name="childdob" select="//Answer[@name='ChildDOB']//DateValue[$getchildren]"/>	
	<xsl:variable name="childaddress1" select="(//Answer[@name='ApplicantAddressLine1']/RptValue/TextValue[position()=1]"/>
	<xsl:variable name="childcity" select="(//Answer[@name='ApplicantCity']/RptValue/TextValue[1]"/>
	<xsl:variable name="childstate" select="//Answer[@name='ApplicantState']/RptValue/TextValue[1]"/>
	<xsl:variable name="childzipcode" select="//Answer[@name='ApplicantZip']/RptValue/TextValue[1]"/>
	<xsl:variable name="childcounty" select="(//Answer[@name='ApplicantCounty']/RptValue/TextValue[1]"/>
	<xsl:variable name="childaddresstype" select="//Answer[@name='ApplicantAddressType']/RptValue/TextValue[1]"/>
	<xsl:variable name="childcityid" select="//Answer[@name='ApplicantCityID']/RptValue/TextValue[1]"/> 
	
	
	<xsl:if test="normalize-space() or normalize-space($getchildren)">
		<xsl:element name="Children">
			<xsl:element name="Child">
				<xsl:element name="ChildFirstName">
					<xsl:value-of select="//Answer[@name='ChildFirstName']//TextValue[$getchildren]"/>
				</xsl:element>
				<xsl:element name="ChildFullName">
					<xsl:value-of select="$childfullname"/>
				</xsl:element>
				<xsl:element name="ChildMiddleInit">
					<xsl:value-of select="$childmiddleinit"/>
				</xsl:element>
				<xsl:element name="ChildLastName">
					<xsl:value-of select="$childlastname"/>
				</xsl:element>
				<xsl:element name="ChildSuffix">
					<xsl:if test="$childsuffix != 'None'">
						<xsl:value-of select="$childsuffix"/>
					</xsl:if>
				</xsl:element>
				<xsl:element name="ChildDOB">
					<xsl:call-template name = "ChangeDateFormat">
						<xsl:with-param name="MDY" select="$childdob"/>
					</xsl:call-template>
				</xsl:element>								
			       </xsl:element>
				<xsl:element name="ChildAddress1">					
					<xsl:value-of select="$childaddress1"/>					
				</xsl:element>
				<xsl:element name="ChildCity">					
					<xsl:value-of select="$childcity"/>					
				</xsl:element>
				<xsl:element name="ChildState">
					<xsl:value-of select="$childstate"/>
				</xsl:element>
				<xsl:element name="ChildZipCode">
					<xsl:value-of select="$childzipcode"/>
				</xsl:element>
				<xsl:element name="ChildCounty">					
					<xsl:value-of select="$childcounty"/>					
				</xsl:element>
				<xsl:element name="ChildAddressType">
					<xsl:value-of select="$childaddresstype"/>
				</xsl:element>
				<xsl:element name="ChildCityID">
					<xsl:value-of select="$childcityid"/>
				</xsl:element> 
		</xsl:element>
	</xsl:if>
</xsl:template>	

Open in new window

The code errors out, of course. I've tried creating a separate address template for the children, but I can't get it to loop through all the children. Can someone point me in the right direction?
chenegarAsked:
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:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="AnswerSet">
        <xsl:apply-templates select="Answer[@name='ChildFirstName']/RptValue/TextValue"></xsl:apply-templates>
    </xsl:template>
    
    


    <xsl:template match="Answer[@name='ChildFirstName']/RptValue/TextValue">
        
        <xsl:variable name="getchildren" select="position()"/>
        
        <xsl:variable name="childfullname" select="//Answer[@name='ChildFullName']//TextValue[$getchildren]"/>
        <xsl:variable name="childmiddleinit" select="//Answer[@name='ChildMiddleInitial']//TextValue[$getchildren]"/>
        <xsl:variable name="childlastname" select="//Answer[@name='ChildLastName']//TextValue[$getchildren]"/>
        <xsl:variable name="childsuffix" select="//Answer[@name='ChildNameSuffix']//MCValue[$getchildren]"/>
        <xsl:variable name="childdob" select="//Answer[@name='ChildDOB']//DateValue[$getchildren]"/>	
        <xsl:variable name="childaddress1" select="(//Answer[@name='ApplicantAddressLine1']/RptValue/TextValue)[position()=1]"/>
        <xsl:variable name="childcity" select="(//Answer[@name='ApplicantCity']/RptValue/TextValue)[1]"/>
        <xsl:variable name="childstate" select="//Answer[@name='ApplicantState']/RptValue/TextValue[1]"/>
        <xsl:variable name="childzipcode" select="//Answer[@name='ApplicantZip']/RptValue/TextValue[1]"/>
        <xsl:variable name="childcounty" select="(//Answer[@name='ApplicantCounty']/RptValue/TextValue)[1]"/>
        <xsl:variable name="childaddresstype" select="//Answer[@name='ApplicantAddressType']/RptValue/TextValue[1]"/>
        <xsl:variable name="childcityid" select="//Answer[@name='ApplicantCityID']/RptValue/TextValue[1]"/> 
        
        
        <xsl:if test="normalize-space() or normalize-space($getchildren)">
            <xsl:element name="Children">
                <xsl:element name="Child">
                    <xsl:element name="ChildFirstName">
                        <xsl:value-of select="//Answer[@name='ChildFirstName']//TextValue[$getchildren]"/>
                    </xsl:element>
                    <xsl:element name="ChildFullName">
                        <xsl:value-of select="$childfullname"/>
                    </xsl:element>
                    <xsl:element name="ChildMiddleInit">
                        <xsl:value-of select="$childmiddleinit"/>
                    </xsl:element>
                    <xsl:element name="ChildLastName">
                        <xsl:value-of select="$childlastname"/>
                    </xsl:element>
                    <xsl:element name="ChildSuffix">
                        <xsl:if test="$childsuffix != 'None'">
                            <xsl:value-of select="$childsuffix"/>
                        </xsl:if>
                    </xsl:element>
                    <xsl:element name="ChildDOB">
                        <xsl:call-template name = "ChangeDateFormat">
                            <xsl:with-param name="MDY" select="$childdob"/>
                        </xsl:call-template>
                    </xsl:element>								
                    <xsl:element name="ChildAddress1">					
                        <xsl:value-of select="$childaddress1"/>					
                    </xsl:element>
                    <xsl:element name="ChildCity">					
                        <xsl:value-of select="$childcity"/>					
                    </xsl:element>
                    <xsl:element name="ChildState">
                        <xsl:value-of select="$childstate"/>
                    </xsl:element>
                    <xsl:element name="ChildZipCode">
                        <xsl:value-of select="$childzipcode"/>
                    </xsl:element>
                    <xsl:element name="ChildCounty">					
                        <xsl:value-of select="$childcounty"/>					
                    </xsl:element>
                    <xsl:element name="ChildAddressType">
                        <xsl:value-of select="$childaddresstype"/>
                    </xsl:element>
                    <xsl:element name="ChildCityID">
                        <xsl:value-of select="$childcityid"/>
                    </xsl:element> 
                </xsl:element>
             </xsl:element>
        </xsl:if>
    </xsl:template>	
    
    <xsl:template name="ChangeDateFormat">
        <xsl:param name="MDY"/>
        <xsl:value-of select="$MDY"/>
    </xsl:template>
</xsl:stylesheet>

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
Gertone (Geert Bormans)Information ArchitectCommented:
you were just missing some closing brackets
and I reordered the elements a bit

I would not use variables but put the calculation in the result tree directly
Gertone (Geert Bormans)Information ArchitectCommented:
Oh, and I added the correct looping template. You need to use it exactly like that, or you will get the counter wrong
chenegarAuthor Commented:
I was so close! I been working at it so long that I had started to doubt I was on the right track. A couple of parens and I'd have almost had it. Thanks!
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
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.