Click to See Complete Forum and Search --> : XSL Help


gjagge
03-15-2004, 05:55 PM
This code works, but I am rather new at this so I believe that there has to be a better way of doing this. I am trying to figure out how many column I am going to put into my table. Before this code I basically have all of these variables that look to see if there is any data in the XML elements, then I either set the variables to On or Off. Then in this code that I show here I am checking to see if the variable is off or on to determine if I have a column or not. Any advice would be appreciated. Thanks


<xsl:variable name="C1">
<xsl:choose>
<xsl:when test="$Mfg = 'True'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C2">
<xsl:choose>
<xsl:when test="$Spec1 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C3">
<xsl:choose>
<xsl:when test="$Spec2 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C4">
<xsl:choose>
<xsl:when test="$Spec3 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C5">
<xsl:choose>
<xsl:when test="$Spec4 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C6">
<xsl:choose>
<xsl:when test="$Spec5 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C7">
<xsl:choose>
<xsl:when test="$Spec6 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C8">
<xsl:choose>
<xsl:when test="$Spec7 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C9">
<xsl:choose>
<xsl:when test="$Spec8 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C10">
<xsl:choose>
<xsl:when test="$Spec9 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C11">
<xsl:choose>
<xsl:when test="$Spec10 != 'Off'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C12">
<xsl:choose>
<xsl:when test="$Weight = 'On'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C13">
<xsl:choose>
<xsl:when test="$Unit = 'On'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="C14">
<xsl:choose>
<xsl:when test="$Price = '1'">
<xsl:value-of select="number(1)"/>
</xsl:when>
<xsl:when test="$Price = '2'">
<xsl:value-of select="number(1)"/>
</xsl:when>
<xsl:when test="$Price = '4'">
<xsl:value-of select="number(1)"/>
</xsl:when>
<xsl:when test="$Price = '3'">
<xsl:value-of select="number(2)"/>
</xsl:when>
<xsl:when test="$Price = '5'">
<xsl:value-of select="number(2)"/>
</xsl:when>
<xsl:when test="$Price = '6'">
<xsl:value-of select="number(2)"/>
</xsl:when>
<xsl:when test="$Price = '7'">
<xsl:value-of select="number(3)"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="NbrCols" select="sum($C1+$C2+$C3+$C4+$C5+$C6+$C7+$C8+$C9+$C10+$C11+$C12+$C13+$C14)"/>

Hp108
03-16-2004, 01:05 PM
Are you talking about html table? if so,
why check if the element has any data...

a construct like this should give you a dynamic table....



<table>

<!--first row-->
<tr>
<xsl:for-each select="xpath expression to select subnodes">
<xsl:if test="sub-node!= ''">
<td>
<xsl:value-of select="subnode">
</td>
</xsl:if>
</xsl:for-each>
</tr>

</table>



Of course that wil give you the first row....
There is more than one approach you could use....


Or if your xml does not have the element present when there is no data, you ould use template match..

You might want to use the count() function to count the elements that have data...


Without the xml structure and knowing exactly what your output should be, it is not easy to give you elaborate suggestions...
Hope this helps....

gjagge
03-16-2004, 04:06 PM
Hey HP108,
I have tried to apply what you have suggested but didn’t have any success. I have attached an example XML file along with the XSL file and a sample of the MIF code that I am generating. I guess what I was trying to accomplish was to look at the element to see if it contained data, if it does I would collect the data in the variable, if it doesn’t I would set the variable to off. At the same time, I was also hoping I could count the number of active elements I have. I guess that I am thinking that this will be a more efficient way of coding, to go threw the XML data once and collect what you need, not to go over the XML repeatedly. But I am new to XML and developing, so I could be wrong.

Thanks for your help.

gjagge
03-16-2004, 04:09 PM
Didn't know if my attachment got posted

Hp108
03-17-2004, 08:34 AM
I quickly looked at your code and here is a link (http://www.xml.com/lpt/a/2002/07/31/qa.html) to a solution that might work for you...It uses the Muenchian method for grouping data. You'll have to implement it to your needs, hope it works for you...Other than that I have no ideas...maybe someone else here might have aanother quicker solution.

Good luck!

gjagge
03-17-2004, 09:15 AM
Thanks for all your help Hp.