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 December 11th, 2009, 08:46 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default loop over similiar tags

Hello all,
Using XSLT 1.0
I have the following xml structure below. There must always be the first <DEWS tag, but the rest are optional. If there was a second dews tag it would be dews2 and so on. Now how do i loop over these tags in a for-each statement? Is this possible?

Code:
 <record>
        
        <DEWS1>DC22</DEWS1>
<DEWS2>DC23</DEWS1>
<DEWS3>DC24</DEWS1>
<DEWS4>DC25</DEWS1>
<DEWS5>DC26</DEWS1>
</record
Help would be much appreciated

Regards.
 
Old December 11th, 2009, 08:51 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Doesn't
Code:
<xsl:for-each select="/record/*">...</xsl:for-each>
suffice?
Or you could use
Code:
<xsl:for-each select="/record/*[starts-with(local-name(), 'DEWS')]">...</xsl:for-each>
if there are other child elements of 'record' you don't want to process.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old December 11th, 2009, 09:21 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Thanks Martin,

yeah there are other children so the second one is needed. I forgot about local-name.

Regards.
 
Old December 11th, 2009, 09:54 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Hi, I am trying to get the first preceding value of CR for the type value
But it just seems to return the same value for both author records. Any ideas?

Code:
                        <xsl:for-each select="*[starts-with(local-name(), 'ICFN')]">
                            <author>
                                <name><xsl:value-of select="concat(.,', ',following-sibling::*[1])"/></name>
                                <type><xsl:value-of select="preceding-sibling::*[starts-with(local-name(),'CR')]"/></type>
                            </author>
                        </xsl:for-each>


<record>
<CR1>asdfasdf</CR1>
        <CRT1>Edited by</CRT1>
        <CCI1>N</CCI1>
        <ICFN1>Adnan</ICFN1>
        <ICKN1>Ibrahimbegovic</ICKN1>
        <CR2>B01</CR2>
        <CRT2>Edited by</CRT2>
        <CCI2>N</CCI2>
        <ICFN2>Muhamed</ICFN2>
        <ICKN2>Zlatar</ICKN2>
</record>
Regards,

John.
 
Old December 11th, 2009, 09:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

preceding-sibling::*[starts-with(local-name(),'CR')][1]

And it probably doesn't help that the value is the SAME for both records.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old December 11th, 2009, 09:58 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

That snippet you posted has a 'CRT1' element and a 'CRT2' element, but both have the same contents 'Edited by' so I am not sure why you complain about getting the same value.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old December 11th, 2009, 10:13 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

That was just a copy and paste error in the xml, normally they have different values. Thanks a lot
 
Old December 11th, 2009, 10:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a horrible XML design. Do you have any control over it? There seems to be a sequence of XXX1 elements representing the first record, then a sequence of XXX2 elements representing the second record, and so on: so put each of these sequences inside a wrapper element, and use the same tags in each one.

If you can't get the design of the XML changed at source, it's worth transforming it into something more sanitory before you attempt any other kind of transformation. Then you only have to write the "sanitization" code once - everthing else will be able to start with clean input.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 11th, 2009, 10:52 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

No i have no control over it. It is the Nielsen BookData Web Service: XML Format Field Specification
Regards.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
Adding similiar occurences with loop musman0047 XSLT 7 February 18th, 2009 12:18 AM
xslt for tags dnandhak XSLT 4 May 23rd, 2008 05:57 AM
How to read tags? FathimaSagar ASP.NET 1.0 and 1.1 Professional 1 January 14th, 2004 02:07 PM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM





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