CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   XML (http://www.codingforums.com/forumdisplay.php?f=3)
-   -   Weird XSLT returns -- for-each/select duplicating data? (http://www.codingforums.com/showthread.php?t=277672)

joeldberry 10-18-2012 09:13 PM

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

sunfighter 10-19-2012 05:10 PM

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.

joeldberry 10-19-2012 08:08 PM

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


All times are GMT +1. The time now is 03:04 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.