Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 22nd, 2013, 11:32 AM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Lightbulb Iterating over child nodes

This seems simple enough but I can't seem to get it to work.
My xml looks like this:

HTML Code:
<bsar:BSAForm>
    <bsar:SubjectInformation>
        <bsar:LastNameOrNameOfEntity> Obama</bsar:LastNameOrNameOfEntity>
        <bsar:AddressBlock>
            <ucc:Address> 9 Dale Rd</ucc:Address>
            <ucc:City> Woodbury</ucc:City>
        </bsar:AddressBlock>
        <bsar:AddressBlock>
            <ucc:Address> 123 Fake St</ucc:Address>
            <ucc:City> Springfield</ucc:City>
        </bsar:AddressBlock>
    </bsar:SubjectInformation>
</bsar:BSAForm>
I need to iterate over both of these elements and display the content. My XSLT looks like this:

HTML Code:
<xsl:template match=/bsar:BSAForm">
     <xsl:apply-templates select="bsar:SubjectInformation"/>
</xsl:template>

<xsl:template match="bsar:SubjectInformation">
    <xsl:text xml:space="preserve">4A</xsl:text>
    <xsl:text xml:space="preserve">00001</xsl:text>
    <xsl:value-of select="bsar:LastNameOrNameOfEntity"/>
    <xsl:value-of select="'&#xD;&#xA;'"/> <!-- new line -->

    <xsl:apply-templates select="bsar:AddressBlock"/>
</xsl:template>

<xsl:template match="bsar:AddressBlock">

    <xsl:variable name="Addr" select="../bsar:AddressBlock"/>

    <xsl:text xml:space="preserve">4B</xsl:text>
    <xsl:text xml:space="preserve">00001</xsl:text>
    <xsl:value-of select="$Addr/ucc:Address"/>
    <xsl:value-of select="$Addr/ucc:City"/>
    <xsl:value-of select="'&#xD;&#xA;'"/> <!-- new line -->
</xsl:template>
The output should be as follows:
Code:
4A 00001 Obama
4B 9 Dale Rd Woodbury
4B 123 Fake St Springfield
But instead, the output is coming out like this:

Code:
4A 00001 Obama
4B 9 Dale Rd Woodbury
4B 9 Dale Rd Woodbury
I have tried many different ways to do this, using a for each, using a for each like this:

HTML Code:
<xsl:variable name="header" select="."/>
 <xsl:for-each select="following-sibling::bsar:TelephoneBlock[ancestor::bsar:SubjectInformation[1] = $header]">
or even passing in a counter from a for-loop and using that to access the specified element like this:

HTML Code:
<xsl:for-each select="bsar:TelephoneBlock">
    <xsl:variable name="Index">
        <xsl:number count="bsar:TelephoneBlock" />
    </xsl:variable>

    <xsl:call-template name="SubjectPhone">
        <xsl:with-param name="$Index"/>
    </xsl:call-template>
</xsl:for-each>

<xsl:template name="SubjectPhone">
    <xsl:param name="Index"/>

    <xsl:variable name="Telephone" select="../bsar:TelephoneBlock[$Index]"/>
    ...
</xsl:template>
In all these cases, it's displaying the first address twice. Pleae let me know if you notice anything I am doing wrong.
Thanks in advance.

Last edited by Dracarnion; February 22nd, 2013 at 11:38 AM..
 
Old February 22nd, 2013, 12:33 PM
Authorized User
 
Join Date: Apr 2006
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

For anyone here, this was answered on another forum:

http://stackoverflow.com/questions/1...er-child-nodes





Similar Threads
Thread Thread Starter Forum Replies Last Post
template for child nodes pushkar_joshi XSLT 7 August 12th, 2010 06:02 AM
not able to print out the child nodes eruditionist XSLT 7 October 30th, 2008 10:33 AM
Retrieveal of child nodes.... nathilson21 Classic ASP XML 0 May 7th, 2007 04:54 AM
counting child nodes Tomi XSLT 1 September 6th, 2006 03:26 AM
Iterating thru all child items of selected parent mahesh_ys@indiatimes.com Beginning VB 6 0 May 3rd, 2004 02:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.