Jump to content

Using attributes in XSL


trainzebra

Recommended Posts

Both of my files are rather large, so I'm going to attempt and only pull out the relevant information. The involved xml segments look like this.

<PLMXML><Person id="id18" lastName="Mike Jones">...<UserData id="id29"></Person></PLMXML>

I'm attempting to simply pull the lastName attribute out of the file and display it. The code here is not working, though most examples that I've looked at suggest it should.

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">...<tr>		<td align="left"><font size="3" color="000000" face="arial">Initiator: <xsl:value-of select="PLMXML/Person/@lastName"/></font></td></tr>

If I make the following change, it displays correctly, but this seems to be a bandaid solution to me.

<tr>		  <td align="left"><font size="3" color="000000" face="arial">Initiator: <xsl:value-of select="//@lastName"/></font></td></tr>

Later on in my file I'm attempting to check and see if a node has a targetRefs attribute. The relevant xml is as follows.

<PLMXML><WorkflowProcess id="id15" name="A-COC-6-23419"><Task id="id59" name="ReverseEnggProcess" state="pending" targetRefs="#id2 #id60 #id13"></Task></WorkflowProcess></PLMXML>

Here is the xsl. My assumption is that it's failing to pass through the xsl:when condition as nothing is being displayed at all. Is this the proper syntax for it?

<xsl:for-each select="PLMXML/WorkflowProcess">		<tr>			<xsl:for-each select="Task">					 <xsl:choose>						<xsl:when test="@targetRefs">							<!--if it has targetRefs, then its the top level task, don't display anything-->							<xsl:if test="@state='started'">									<td><img src="blue1.jpg"></img></td>									<td><font size="2" color="000000" face="arial"><xsl:value-of select="../@name"/></font></td>									<td colspan="5"><hr></hr></td>							</xsl:if>					   </xsl:when>...					</xsl:choose></xsl:for-each></xsl:for-each>

Can anyone see any obvious problems? I hope I've been clear on this one, been staring at the screen a while and it's starting to get to me :)

Link to comment
Share on other sites

The first example is really weird. Are you sure the <tr> is not in some sort of for-each or anything?As for the second example, I think in your case, the best way is this:

<xsl:for-each select="PLMXML/WorkflowProcess">		<tr>			<xsl:for-each select="Task">					 <xsl:choose>						<xsl:when test="@targetRefs and @state='started'">							<!--if it has targetRefs, AND the @state is 'started' do the following-->									<td><img src="blue1.jpg"></img></td>									<td><font size="2" color="000000" face="arial"><xsl:value-of select="../@name"/></font></td>									<td colspan="5"><hr></hr></td>							</xsl:when><!--Other conditions-->...					</xsl:choose></xsl:for-each></tr></xsl:for-each>

For other cases, other methods might be better. It really depends on the case itself.

Link to comment
Share on other sites

Thanks for the response. For the first example, nope, the <tr>s aren't in any sort of loop. Here's the code from start to that point if it helps any. Counter is a variable that's used for table formatting later and pageEdit is a script that's still being worked on, but not relevant at the moment. It's also probably worth noting that I had all of this code and this particular example working with a different version of xsl (http://www.w3.org/TR/WD-xsl) a few days ago, with proper syntax changes of course.

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body>	<script type="text/javascript">			var counter=0;	</script>	<table border="0" cellspacing="10">		<!--get the status report name out of the WorkflowProcess element-->			<tr align="center">				<td align="right"><img src="minilogo.jpg"></img></td>				<td align="left"><font size="5" color="000000" face="arial"> Job Order Report</font></td>			</tr>			<tr>				<td align="left"><font size="3" color="000000" face="arial">Initiator: <xsl:value-of select="PLMXML/Person/@lastName"/></font></td>			</tr>			<tr>				<td align="left"><font size="3" color="000000" face="arial">Reverse Engineering Process</font></td>			</tr>			<tr>				<td>					<form name="filter" method="get" action="">						<select name="list" onChange="pageEdit(this.form)">							<option name="opAll" value="all">All Items</option>							<option name="opCompleted" value="completed">Completed</option>							<option name="opStarted" value="started">Started</option>							<option name="opPending" value="pending">Pending</option>						</select>						<input type="submit" value="Display" onClick="pageEdit(this.form)"></input>					</form>				</td>			</tr>	</table>

In reguards to the second example, that would work except that I need to check for three different conditions once I've determined if the element has target refs or not. Specifically completed, started, or pending. So it ends up being:

<xsl:when test="@targetRefs"><xsl:if test="@state='started'">...</xsl:if><xsl:if test="@state='completed'">...</xsl:if><xsl:if test="@state='pending'">...</xsl:if></xsl:when>

Sorry for not clarifying that sooner. The larger problem with this one is that it doesn't seem to be meeting the when condition to get into the statement. For example if I just hardcode data into the when it won't display. Thanks again for the help.

Link to comment
Share on other sites

I tested the first example and it worked perfectly. The only thing I did was to close the UserData element, because it was ill-forming the XML.For the second example, I think a nested choose looks better and it's also more efficient, scince once the first conditions is true, the next ones won't be checked.So what I suggest is this:

<xsl:when test="@targetRefs"><xsl:choose><xsl:when test="@state='started'">...</xsl:when><xsl:if test="@state='completed'">...</xsl:when><xsl:if test="@state='pending'">...</xsl:when></xsl:choose></xsl:when>

You don't have to have <xsl:otherwise> so this makes the process smoother.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...