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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT - unable to put my logic to code... 2

Status
Not open for further replies.

Sniipe

Programmer
Oct 9, 2006
115
IE
I have XSLT transforming my XML Spreadsheet. Works well except for one part.

On the worksheet my cells would look like this:

HeadingName1
bike1 True
bike2 False

HeadingName2
Car1 True
Car2 False
Car3 True


The XSL looks like this:
Code:
<xsl:for-each select="ss:Table/ss:Row">
				<!-- Get the Element attributes then add if necessary -->
				<xsl:element name="element">
					<xsl:for-each select="ss:Cell">
						<xsl:choose>
							<xsl:when test="position()=1">
								<xsl:attribute name="type">
									<xsl:value-of select="."/>
								</xsl:attribute>
							</xsl:when>
							<xsl:when test="position()=2">
								<xsl:attribute name="name">
									<xsl:value-of select="."/>
								</xsl:attribute>
							</xsl:when>
							<xsl:when test="position()=3">
								<xsl:attribute name="id">
									<xsl:value-of select="."/>
								</xsl:attribute>
							</xsl:when>
							<xsl:otherwise>
								<xsl:attribute name="selected">
									<xsl:value-of select="."/>
								</xsl:attribute>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:for-each>
				</xsl:element>
			</xsl:for-each>

What comes out from that is:
Code:
<ApplicableToTabWorksheet>
   <element name="HeadingName1" id="" selected=""/>
   <element name="Car1" id="1" selected="1"/>
   <element name="Car2" id="2" selected=""/>
   <element name=""/>
   <element name="HeadingName2" id="" selected="Selected"/>
   <element name="car1" id="1" selected="1"/>
   <element name="car2" id="2" selected=""/>
   <element name="car3" id="3" selected="1"/>
</ApplicableToTabWorksheet

What I want is not to output any element with an id=""

Is that possible as we only know if the attribute is "" after we draw out the element tag???


Therefore the desired output would be
Code:
<ApplicableToTabWorksheet>
   <element name="bike1" id="1" selected="1"/>
   <element name="bike2" id="2" selected=""/>
   <element name="car1" id="1" selected="1"/>
   <element name="car2" id="2" selected=""/>
   <element name="car3" id="3" selected="1"/>
</ApplicableToTabWorksheet

note: I relised after that I had 4 attributes... I'm thinking of changing the element to
<element type="mode" name="bike" id="1" selected="1"/>
 
You can wrap the whole construction of "element" in a conditional statement, like this.
[tt]
<xsl:for-each select="ss:Table/ss:Row">
<xsl:if test="string-length(normalize-space(ss:Cell[3])) != 0">
<!-- all the script lines shown -->
</xsl:if>
</xsl:for-each>
[/tt]
 
Thank you tsuji, that fixed my issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top