Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL Stylesheet 1

Status
Not open for further replies.

kyern

Programmer
Mar 30, 2006
36
US
I am trying to create a xml stylesheet that will end up outputing like so:

Code:
<li class="alt"><label for="a1"><input id="a1" name="a1" value="xxx" type="checkbox" />xxx</label></li>
<li><label for="a2"><input id="a2" name="a1" value="xxx" type="checkbox" />xxx</label></li>
<li class="alt"><label for="a3"><input id="a3" value="xxx" name="a1" type="checkbox" />xxx</label></li>
<li><label for="a4"><input id="a4" name="a1" value="xxx" type="checkbox" />xxx</label></li>
<li class="alt"><label for="a5"><input id="a5" value="xxx"name="a1" type="checkbox" />xxx</label></li>
<li><label for="a6"><input id="a6" name="a1" value="xxx" type="checkbox" />xxx</label></li>

The majority of this is easy to work with except for the unique values in for= and id=. I tried to find a good way to increment a value but it doesn't look like it can be done in xsl. Is there any other way i can create these unique values with xsl?
 
Of course it can be done. Whats your xml?

Jon

"I don't regret this, but I both rue and lament it.
 
This is my xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="html" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>
    <xsl:template match="cat">
		<xsl:for-each select="catitem">	
			<li>
				<xsl:if test="position() mod 2 > 0">
					<xsl:attribute name="class">
						<xsl:text>alt</xsl:text>
					</xsl:attribute>
				</xsl:if>
					<label for="a1"><input id="a1" name="a1" type="checkbox">
					<xsl:attribute name="value">
						<xsl:value-of select="abrev"/>
					</xsl:attribute>
					<xsl:value-of select="name"/>
				</input></label></li>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
 
In that case, you can simply use a xsl function that I recently showed its use in another thread. Change the label part like this.
[tt]
<li>
<xsl:if test="position() mod 2 &gt; 0">
<xsl:attribute name="class">
<xsl:text>alt</xsl:text>
</xsl:attribute>
</xsl:if>
<xsl:variable name="sig" select="concat('a',count(preceding::*[name() = name(current())]) + 1)" />
<label for="{$sig}">
<input id="{$sig}" name="a1" type="checkbox">
<xsl:attribute name="value">
<xsl:value-of select="abrev"/>
</xsl:attribute>
<xsl:value-of select="name"/>
</input>
</label>
</li>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top