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

XSL nested Foreach and excluding a node 1

Status
Not open for further replies.

mcowen

Programmer
Oct 21, 2001
134
0
0
GB
I have a XML document with HISTORY and SCHEDULE elements. I need to iterate through the SCHEDULE elements and build a table row. For that schedule there could be 0 or many history elements that I also need to add to the table but without duplicating!

Code:
	<HISTORY>
		<DATE_INSERTED>
			<DAY>16</DAY>
			<MONTH>02</MONTH>
			<YEAR>2006</YEAR>
		</DATE_INSERTED>
		<DESCRIPTION>Office Copies Received</DESCRIPTION>
		<EXTRA_TEXT>&gt;&gt;</EXTRA_TEXT>
		<TIME_USED/>
		<TIME_CHARGE>0.00</TIME_CHARGE>
		<TIME_COST>0.00</TIME_COST>
		<CASE_HOLDER>Melissa Johnson</CASE_HOLDER>
		<USER_ID>melissa</USER_ID>
		<WKTYPE/>
		<DOCUMENT_ID>00003365.HST</DOCUMENT_ID>
		<HISTORY_INT_CODE>0x0004469a</HISTORY_INT_CODE>
		<DATE_AMENDED>
			<DAY>17</DAY>
			<MONTH>02</MONTH>
			<YEAR>2006</YEAR>
		</DATE_AMENDED>
		<TIME_CREATED>10:36:32</TIME_CREATED>
		<HISTORY_NO>37978</HISTORY_NO>
		<POST_TRAY/>
		<DOCUMENT_TYPE>U</DOCUMENT_TYPE>
		<HS30>EOT3</HS30>
		<HS31/>
		<EXTRA_TEXT_VALUE>Copy_of_Registers_at_Land_Registry</EXTRA_TEXT_VALUE>
	</HISTORY>
	<HISTORY>
		<DATE_INSERTED>
			<DAY>16</DAY>
			<MONTH>02</MONTH>
			<YEAR>2006</YEAR>
		</DATE_INSERTED>
		<DESCRIPTION>Other Title Documents received</DESCRIPTION>
		<EXTRA_TEXT>&gt;&gt;</EXTRA_TEXT>
		<TIME_USED/>
		<TIME_CHARGE>0.00</TIME_CHARGE>
		<TIME_COST>0.00</TIME_COST>
		<CASE_HOLDER>Melissa Johnson</CASE_HOLDER>
		<USER_ID>melissa</USER_ID>
		<WKTYPE/>
		<DOCUMENT_ID>00003363.HST</DOCUMENT_ID>
		<HISTORY_INT_CODE>0x00044657</HISTORY_INT_CODE>
		<DATE_AMENDED>
			<DAY>16</DAY>
			<MONTH>02</MONTH>
			<YEAR>2006</YEAR>
		</DATE_AMENDED>
		<TIME_CREATED>10:38:54</TIME_CREATED>
		<HISTORY_NO>37985</HISTORY_NO>
		<POST_TRAY/>
		<DOCUMENT_TYPE>U</DOCUMENT_TYPE>
		<HS30>EOT5</HS30>
		<HS31/>
		<EXTRA_TEXT_VALUE>Decoded_Title_Document</EXTRA_TEXT_VALUE>
	</HISTORY>
	<SCHEDULE>
		<Activity_code>EOT2-1</Activity_code>
		<Description>Title Number Retrieved</Description>
		<Target>
			<Day>16</Day>
			<Month>02</Month>
			<Year>2006</Year>
		</Target>
		<Actual>
			<Day>16</Day>
			<Month>02</Month>
			<Year>2006</Year>
		</Actual>
		<Deliverable>0.00</Deliverable>
	</SCHEDULE>
	<SCHEDULE>
		<Activity_code>EOT3</Activity_code>
		<Description>Office Copies Received</Description>
		<Target>
			<Day>16</Day>
			<Month>02</Month>
			<Year>2006</Year>
		</Target>
	</SCHEDULE>
	<SCHEDULE>
		<Activity_code>EOT4</Activity_code>
		<Description>Plan received</Description>
		<Target>
			<Day>16</Day>
			<Month>02</Month>
			<Year>2006</Year>
		</Target>
	</SCHEDULE>
	<SCHEDULE>
		<Activity_code>EOT5</Activity_code>
		<Description>Other Title Documents received</Description>
		<Target>
			<Day>20</Day>
			<Month>02</Month>
			<Year>2006</Year>
		</Target>
		<Deliverable>n</Deliverable>
	</SCHEDULE>

I'm using a Foreach to iterate through the schedule then a nested foreach to iterate through the history.
The problem is that when I loop through the history I duplicate the entry I added when processing the schedules. I 've tried to check that the not(DOCUMENT_ID=$docid) but it doesnt seem to equate to true.

Code:
		  <xsl:for-each select="SCHEDULE">
			  <xsl:variable name="activitycode" select="Activity_code"/>
			  <xsl:variable name="docid" select="//HISTORY[HS30=$activitycode]/DOCUMENT_ID"/>
			  <!--script>alert('<xsl:value-of select="$docid"/>');</script-->
					<tr>
						<td><xsl:value-of select="Description"/></td>
					</tr>
			  <xsl:variable name="history" select="//HISTORY[HS30=$activitycode]"/>
			  <xsl:for-each select="$history">
			  <xsl:if test="not($history/DOCUMENT_ID)=$docid)">
				  <script>alert('<xsl:value-of select="$docid"/>  <xsl:value-of select="DOCUMENT_ID"/> ');</script>
				  <tr>
				  	<td height="22" valign="middle">
							<a><xsl:attribute name="href">?function=document_run&amp;document=DELIVERABLE&amp;owner_code=<xsl:value-of select="$owner_int_code"/>&amp;owner_type=case&amp;xsl=activities&amp;field@code_TF01=<xsl:value-of select="$activitycode"/>&amp;case_int_code=<xsl:value-of select="$owner_int_code"/>&amp;act=<xsl:value-of select="DESCRIPTION"/>&amp;sale=<xsl:value-of select="$sale"/>&amp;return_params=true&amp;session_key=<xsl:value-of select="$sessionkey"/>&amp;name=<xsl:value-of select="$name"/>&amp;role=<xsl:value-of select="$role"/></xsl:attribute><xsl:value-of select="DESCRIPTION"/><xsl:if test="not(EXTRA_TEXT_VALUE='')">&#160;-&#160;<xsl:value-of select="EXTRA_TEXT_VALUE"/></xsl:if></a>
					</td>
				  </tr>
			  </xsl:if>
			  </xsl:for-each>
		  </xsl:for-each>

I cant get not($history/DOCUMENT_ID)=$docid) to evaluate as I would expect. I thought it would mean that if a history item is found to have the document id of the schedule added then dont allow it to be added. Does anyone know what I might be doing wrong or can suggest and alternative please? I'd be very grateful.

I've also tried using a key and generate-id() but I'm not that experienced with them.

Thanks

Matt
 
It is not clear what you are trying to achieve. What do you want the output to look like? Why will you have duplicates if your for-each only matches the HISTORY with the same activity code?

Jon

"I don't regret this, but I both rue and lament it.
 
>I cant get not($history/DOCUMENT_ID)=$docid) to evaluate as I would expect.

A comparison on node(-set) would have its own behaviour. Rather, in this case, use string comparison.

>[tt]<xsl:if test="not($history/DOCUMENT_ID[highlight])[/highlight]=$docid)">[/tt]
(note aside: the highlighted may be just a typo, but it shouldn't change behaviour after corrected it.)
[tt]<xsl:if test="not([red]string([/red]$history/DOCUMENT_ID[red])[/red]=[red]string([/red]$docid[red])[/red])">[/tt]

Without see the ancestor part, I guess //HISTORY... might be improved by replacing it to ..//HISTORY... or alike so that you won't go matching HISTORY from it's parent's sibling. Just a thought.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top