Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: position() always 1 in for-each [Thread Next] Re: position() always 1 in for-eachTo: NULL Date: 11/14/2008 9:05:00 AM You say you've solved it but here are a few points:
* Your XML was not well-formed. If you remove the extra root tag then your
original code will work, not sure if that's what you meant
* You are better off getting into the habit of using templates rather than
for-each, they are more maintainable and get you out of the procedural mind
set
* You can simplify the code in general by using Attribute Value Templates,
take a look online
* Your xsl:choose means a lot of duplicated code, always a bad thing
I have rewritten the stylesheet to show these points based on the following
XML:
<root>
<user>
<account>1234</account>
<name>Joe Smith</name>
<neverexp>True</neverexp>
</user>
<user>
<account>5678</account>
<name>Jane Brown</name>
<neverexp>False</neverexp>
</user>
</root>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="root/user" />
</xsl:template>
<xsl:template match="user">
<tr>
<td><xsl:value-of select="account" /></td>
<td><xsl:value-of select="name"/></td>
<td align="center">
<input type="radio" name="{position()}"><!-- attribute value template
used for 'name attribute' -->
<xsl:if test="neverexp='True'">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
</input>
</td>
<td align="center">
<input type="radio" name="{position()}"></input>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
--
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
"Janette" <jtowell@r...> wrote in message
news:#R9lAHgRJHA.4916@T......
> It took a lunch break for me to realise, that I was calling the code that
> built the xml multiple times, and therefore it never had more than one
> user node per call. Instead now I save up the resulting xml till the end
> and then call transform and it correctly increments position().
>
> Sorry for the wasted post.
> Janette
>
>
> "Janette" <jtowell@r...> wrote in message
> news:ehyz31fRJHA.1164@T......
>> Hi,
>>
>> Firstly I apologise in advance I only just started using xsl yesterday.
>> Am I am a beginner with XML. I have tried to find the answer to my
>> problem on the web but just can't.
>>
>> I want to output as a table all user nodes. Including a radio button
>> collection with distinct names for each row. I have tried to use
>> position() as I understood that this gives the position of the node that
>> is being processed, but it always comes back as 1. I am guessing that my
>> problem is something to do with the template match and for each
>> combination but I can't work it out.
>>
>> ie I want as output
>> <tr><td>1234</td><td>Joe Smith</td><td><input type="radio"
>> checked="checked" name="1" /></td><td><input type="radio"
>> name="1"/></td></tr>
>> <tr><td>5678</td><td>Jane Brown</td><td><input type="radio" name="2"
>> /></td><td><input type="radio" checked="checked" name="2"/></td></tr>
>>
>> but instead I get as output
>> <tr><td>1234</td><td>Joe Smith</td><td><input type="radio"
>> checked="checked" name="1" /></td><td><input type="radio"
>> name="1"/></td></tr>
>> <tr><td>5678</td><td>Jane Brown</td><td><input type="radio" name="1"
>> /></td><td><input type="radio" checked="checked" name="1"/></td></tr>
>>
>> XML file looks like this
>>
>> <root>
>> <user>
>> <account>1234</account>
>> <name>Joe Smith</name>
>> <neverexp>True</neverexp>
>> </user>
>> <root>
>> <user>
>> <account>5678</account>
>> <name>Jane Brown</name>
>> <neverexp>False</neverexp>
>> </user>
>> </root>
>>
>> xsl file looks like this
>> <?xml version="1.0" encoding="ISO-8859-1"?>
>> <xsl:stylesheet version="1.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>> <xsl:template match="/">
>> <xsl:for-each select="root/user">
>> <tr>
>> <td><xsl:value-of select="account" /></td>
>> <td><xsl:value-of select="name"/></td>
>> <xsl:choose>
>> <xsl:when test="neverexp='True'">
>> <td align="center">
>> <input type="radio" checked="checked">
>> <xsl:attribute name="name"><xsl:value-of
>> select="position()"/></xsl:attribute>
>> </input>
>> </td>
>> <td align="center">
>> <input type="radio">
>> <xsl:attribute name="name"><xsl:value-of
>> select="position()"/></xsl:attribute>
>> </input>
>> </td>
>> </xsl:when>
>> <xsl:otherwise>
>> <td align="center">
>> <input type="radio">
>> <xsl:attribute name="name"><xsl:value-of
>> select="position()"/></xsl:attribute>
>> </input>
>> </td>
>> <td align="center">
>> <input type="radio" checked="checked">
>> <xsl:attribute name="name"><xsl:value-of
>> select="position()"/></xsl:attribute>
>> </input>
>> </td>
>> </xsl:otherwise>
>> </xsl:choose>
>> </tr>
>> </xsl:for-each>
>> </xsl:template>
>> </xsl:stylesheet>
>>
>> Thanks in advance for your assistance.
>> Janette
>>
>
>
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
