Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-18-2012, 09:13 PM   PM User | #1
joeldberry
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
joeldberry is an unknown quantity at this point
Weird XSLT returns -- for-each/select duplicating data?

I am completely new to XSLT processing, but I have to admit, I am baflled by this situation. It seems that my XSLT <for-each select>s are working only two out of three times on the same xml file using a similar, repetitive structure.

My XSLT:
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
    <xsl:template match="/demographics">
        <html>
            <head>
                <link href="demos2013.css" rel="stylesheet" type="text/css" />
            </head>
            <body>
                <table border="0" cellpadding="5" cellspacing="5">
                    <xsl:for-each select="groups/group">
                        <tr>
                            <td colspan="3" align="left">
                                <b><xsl:value-of select="group-description"/></b>
                            </td>                        
                            <td align="right">
                                <b><xsl:value-of select="group-datatext"/></b>
                            </td>
                            <td align="right">
                                <b><xsl:value-of select="group-percenttext"/></b>
                            </td>
                        </tr> 
                        <xsl:for-each select="sections/section">
                            <tr>
                                <td width="30"></td>
                                <td colspan="2" align="left">
                                    <xsl:value-of select="section-description"/>
                                </td>
                                <td align="right">
                                    <xsl:value-of select="section-datatext"/>
                                </td>
                                <td align="right">
                                    <xsl:value-of select="section-percenttext"/>
                                </td>
                            </tr>
                            <xsl:for-each select="items/item">
                                <tr>
                                    <td width="30"></td>
                                    <td width="30"></td>
                                    <td align="left">
                                        <xsl:value-of select="item-description"/>
                                    </td>
                                    <td align="right">
                                        <xsl:value-of select="item-datatext"/>
                                    </td>
                                    <td align="right">
                                        <xsl:value-of select="item-percenttext"/>
                                    </td>
                                </tr>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                </table>                
            </body>            
        </html>
    </xsl:template>
</xsl:stylesheet>
A snippet of XML that isn't working:
Code:
<group id="2013EstPopHispOrLatinoByOrigin" row="27" calc="False" datatype="dtInteger"
         percenttype="dtUnknown">
         <group-description>2013 Est. Pop Hisp or Latino by Origin</group-description>
         <group-originaldescription>2013 Est. Pop Hisp or Latino by Origin</group-originaldescription>
         <group-datatext>56,981</group-datatext>
         <group-datavalue>56981</group-datavalue>
         <group-percenttext/>
         <group-percentvalue>0</group-percentvalue>
         <sections>
            <section id="NotHispanicOrLatino" row="28" calc="False" datatype="dtInteger"
               percenttype="dtPercent">
               <section-description>Not Hispanic or Latino</section-description>
               <section-originaldescription>Not Hispanic or Latino</section-originaldescription>
               <section-datatext>46,842</section-datatext>
               <section-datavalue>46842</section-datavalue>
               <section-percenttext>82.21</section-percenttext>
               <section-percentvalue>82.206349484916</section-percentvalue>
               <items/>
            </section>
            <section id="HispanicOrLatino" row="29" calc="False" datatype="dtInteger"
               percenttype="dtPercent">
               <section-description>Hispanic or Latino:</section-description>
               <section-originaldescription>Hispanic or Latino:</section-originaldescription>
               <section-datatext>10,139</section-datatext>
               <section-datavalue>10139</section-datavalue>
               <section-percenttext>17.79</section-percenttext>
               <section-percentvalue>17.793650515084</section-percentvalue>
               <items>
                  <item id="Mexican" row="30" calc="False" datatype="dtInteger"
                     percenttype="dtPercent">
                     <item-description>Hispanic or Latino:</item-description>
                     <item-originaldescription>Hispanic or Latino:</item-originaldescription>
                     <item-datatext>10,139</item-datatext>
                     <item-datavalue>10139</item-datavalue>
                     <item-percenttext>17.79</item-percenttext>
                     <item-percentvalue>17.793650515084</item-percentvalue>
                  </item>
                  <item id="PuertoRican" row="31" calc="False" datatype="dtInteger"
                     percenttype="dtPercent">
                     <item-description>Hispanic or Latino:</item-description>
                     <item-originaldescription>Hispanic or Latino:</item-originaldescription>
                     <item-datatext>10,139</item-datatext>
                     <item-datavalue>10139</item-datavalue>
                     <item-percenttext>17.79</item-percenttext>
                     <item-percentvalue>17.793650515084</item-percentvalue>
                  </item>
                  <item id="Cuban" row="32" calc="False" datatype="dtInteger"
                     percenttype="dtPercent">
                     <item-description>Hispanic or Latino:</item-description>
                     <item-originaldescription>Hispanic or Latino:</item-originaldescription>
                     <item-datatext>10,139</item-datatext>
                     <item-datavalue>10139</item-datavalue>
                     <item-percenttext>17.79</item-percenttext>
                     <item-percentvalue>17.793650515084</item-percentvalue>
                  </item>
                  <item id="AllOtherHispanicOrLatino" row="33" calc="False" datatype="dtInteger"
                     percenttype="dtPercent">
                     <item-description>Hispanic or Latino:</item-description>
                     <item-originaldescription>Hispanic or Latino:</item-originaldescription>
                     <item-datatext>10,139</item-datatext>
                     <item-datavalue>10139</item-datavalue>
                     <item-percenttext>17.79</item-percenttext>
                     <item-percentvalue>17.793650515084</item-percentvalue>
                  </item>
               </items>
            </section>
         </sections>
     </group>
The flawed output of this section:
2013 Est. Pop Hisp or Latino by Origin 56,981
Not Hispanic or Latino 46,842 82.21
Hispanic or Latino: 10,139 17.79
Hispanic or Latino: 10,139 17.79
Hispanic or Latino: 10,139 17.79
Hispanic or Latino: 10,139 17.79
Hispanic or Latino: 10,139 17.79


The problem:
Notice how the <item> elements within the <items> element are being duplicated, instead of distinctively processed. This is very odd, since in the XSLT, the very same code works for the group/groups and section/sections element used directly above it. I process it using MSXSL or IE9 or Firefox 14, but get the same results... Am I overlooking something simple?

Thanks in advance!

jdb

You can see the processed file in action (3rd Group Down) at:
http://www.theretailcoach.net/downlo..._RTA_Demos.xml

Right click on these files to view the sources:
http://www.theretailcoach.net/downlo..._RTA_Demos.xml
http://www.theretailcoach.net/downlo.../demos2013.xsl
http://www.theretailcoach.net/downlo.../demos2013.css
joeldberry is offline   Reply With Quote
Old 10-19-2012, 05:10 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
The flawed output of this section: is really not flawed!
It is printing what is in the xml.
Take a close look at it. The same information is in each of the four 'item' sections.

So you have no problem.

FYI I use "XML Notepad" to view xml and check the output. It's not bad and it's free. Give it a go.
sunfighter is offline   Reply With Quote
Old 10-19-2012, 08:08 PM   PM User | #3
joeldberry
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
joeldberry is an unknown quantity at this point
Well, duh!

Sunfighter, you are great!

Thanks so much!

This is one of those "garbage in, garbage out" scenarios we tend to forget can wreck havoc!

jdb
joeldberry is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:22 AM.


Advertisement
Log in to turn off these ads.