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

How do I find the Plea Event Sequence with highest number

Status
Not open for further replies.

momo2000

Programmer
Jan 2, 2015
63
US
What I am trying to do is to use InternalOffenseHistoryID to find and display Plea's ChargeID. I need to find the PleaEventSequence with the highest number (example 2) in ChargeHistory and then find the Plea's InternalOffenseHistoryID that matches that ChargeHistory's InternalOffenseHistoryID.

How do I do this?

Here is my xml doc
XML:
<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="[URL unfurl="true"]http://tsgweb.com"[/URL] xmlns:IXML="[URL unfurl="true"]http://tsgweb.com"[/URL] xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="ChargeDispSent" MessageID="57834892" xmlns="">
	<ControlPoint Timestamp="1/7/2015 9:15:07 AM" UserID="CunninghamC">SAVE-CR-PLEA</ControlPoint>
	<Case InternalID="1616760662" ID="11683904" xmlns:user="[URL unfurl="true"]http://tylertechnologies.com">[/URL]
		<PleaEvent Date="01/07/2015" ID="151480108" InternalPleaEventID="1714733342" xmlns:reslib="urn:reslib">
			<PleaEventDate>01/07/2015</PleaEventDate>
			<TimestampCreate>01/07/2015 09:14:48:533</TimestampCreate>
			<PleaAmendmentReason Word="PLA">Plea agreement</PleaAmendmentReason>
			<Plea ChargeID="10485838" ChargeHistoryID="41274044" InternalChargeID="1616399897" InternalOffenseHistoryID="1635787112">
				<PleaType Word="GUI">Guilty</PleaType>
			</Plea>
			<PleaEvent Date="01/07/2015" ID="151480102" InternalPleaEventID="1714733336">
				<PleaEventDate>01/07/2015</PleaEventDate>
				<TimestampCreate>01/07/2015 09:10:29:813</TimestampCreate>
				<Plea ChargeID="10485838" ChargeHistoryID="41274038" InternalChargeID="1616399897" InternalOffenseHistoryID="1635787106">
					<PleaType Word="NGUI">Not guilty</PleaType>
				</Plea>
				<Plea ChargeID="10485839" ChargeHistoryID="41274039" InternalChargeID="1616399898" InternalOffenseHistoryID="1635787107">
					<PleaType Word="NGUI">Not guilty</PleaType>
				</Plea>
			</PleaEvent>
		</PleaEvent>
		<PleaEvent Op="A" Date="01/07/2015" ID="151480109" InternalPleaEventID="1714733343" xmlns:reslib="urn:reslib">
			<PleaEventDate Op="A">01/07/2015</PleaEventDate>
			<TimestampCreate Op="A">01/07/2015 09:15:07:360</TimestampCreate>
			<PleaAmendmentReason Op="A" Word="PLA">Plea agreement</PleaAmendmentReason>
			<Plea Op="A" ChargeID="10485839" ChargeHistoryID="41274045" InternalChargeID="1616399898" InternalOffenseHistoryID="1635787113">
				<PleaType Op="A" Word="GUI">Guilty</PleaType>
			</Plea>
			<PleaEvent Op="E" Date="01/07/2015" ID="151480103" InternalPleaEventID="1714733337">
				<PleaEventDate>01/07/2015</PleaEventDate>
				<TimestampCreate>01/07/2015 09:11:13:770</TimestampCreate>
				<PleaAmendmentReason Word="AMENDC">Amended complaint</PleaAmendmentReason>
				<Plea ChargeID="10485839" ChargeHistoryID="41274040" InternalChargeID="1616399898" InternalOffenseHistoryID="1635787108">
					<PleaType Word="NONE">None</PleaType>
				</Plea>
				<PleaEvent Date="01/07/2015" ID="151480102" InternalPleaEventID="1714733336">
					<PleaEventDate>01/07/2015</PleaEventDate>
					<TimestampCreate>01/07/2015 09:10:29:813</TimestampCreate>
					<Plea ChargeID="10485838" ChargeHistoryID="41274038" InternalChargeID="1616399897" InternalOffenseHistoryID="1635787106">
						<PleaType Word="NGUI">Not guilty</PleaType>
					</Plea>
					<Plea ChargeID="10485839" ChargeHistoryID="41274039" InternalChargeID="1616399898" InternalOffenseHistoryID="1635787107">
						<PleaType Word="NGUI">Not guilty</PleaType>
					</Plea>
				</PleaEvent>
			</PleaEvent>
		</PleaEvent>
		<Charge ID="10485838" PartyID="16548350" InternalChargeID="1616399897" InternalPartyID="1614451487" xmlns:reslib="urn:reslib">
			<ChargeOffenseDate>01/06/2015</ChargeOffenseDate>
			<ChargeHistory ChargeHistoryID="41274035" Stage="Arrest" ArrestSequence="1" InternalOffenseHistoryID="1635787103">
				<ChargeNumber>1</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274034" Stage="Case Filing" FilingSequence="1" InternalOffenseHistoryID="1635787102">
				<ChargeNumber>1</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274038" Stage="Plea Event" PleaEventSequence="1" InternalOffenseHistoryID="1635787106">
				<ChargeNumber>1</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274044" Stage="Plea Event" PleaEventSequence="2" CurrentCharge="true" InternalOffenseHistoryID="1635787112">
				<ChargeNumber>1</ChargeNumber>
			</ChargeHistory>
		</Charge>
		<Charge Op="E" ID="10485839" PartyID="16548350" InternalChargeID="1616399898" InternalPartyID="1614451487" xmlns:reslib="urn:reslib">
			<ChargeOffenseDate>01/06/2015</ChargeOffenseDate>
			<DateOnOrAbout>false</DateOnOrAbout>
			<ChargeHistory ChargeHistoryID="41274037" Stage="Arrest" ArrestSequence="1" InternalOffenseHistoryID="1635787105">
				<ChargeNumber>2</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274036" Stage="Case Filing" FilingSequence="1" InternalOffenseHistoryID="1635787104">
				<ChargeNumber>2</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274041" Stage="Case Filing" FilingSequence="2" InternalOffenseHistoryID="1635787109">
				<ChargeNumber>2</ChargeNumber>
				<AmendedDate>01/07/2015</AmendedDate>
				<AmendedReason Word="RED">Reduced</AmendedReason>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274043" Stage="Case Filing" FilingSequence="3" InternalOffenseHistoryID="1635787111">
				<ChargeNumber>2</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274039" Stage="Plea Event" PleaEventSequence="1" InternalOffenseHistoryID="1635787107">
				<ChargeNumber>2</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory ChargeHistoryID="41274040" Stage="Plea Event" PleaEventSequence="2" InternalOffenseHistoryID="1635787108">
				<ChargeNumber>2</ChargeNumber>
			</ChargeHistory>
			<ChargeHistory Op="A" ChargeHistoryID="41274045" Stage="Plea Event" PleaEventSequence="3" CurrentCharge="true" InternalOffenseHistoryID="1635787113">
				<ChargeNumber Op="A">2</ChargeNumber>
			</ChargeHistory>
		</Charge>
	</Case>
	<IntegrationConditions>
		<IntegrationCondition Word="CHARGE" Description="Charge">
			<NotificationEvent notificationType="ChargeNotification" elementState="PreDisposition" elementName="Charge" elementKey="10485839">PleaAmended</NotificationEvent>
		</IntegrationCondition>
	</IntegrationConditions>
</Integration>

I am using the following a xslt template
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?altova_samplexml file:///Z:/Training%20with%20Tim%20XML%20code%20various/PleaFix.xml?>
<xsl:stylesheet version="3.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL] xmlns:fn="[URL unfurl="true"]http://www.w3.org/2005/xpath-functions">[/URL]
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<xsl:variable name="vPleaEventID">
			<xsl:call-template name="GetChargePleaEventID">
				<xsl:with-param name="pChargeID">10485838</xsl:with-param>
			</xsl:call-template>
		</xsl:variable>
		<result>
			<xsl:value-of select="$vPleaEventID"/>
		</result>
	</xsl:template>
	<!--	
	-->
	<xsl:template name="GetChargePleaEventID">
		<xsl:param name="pChargeID"/>
		<!--<xsl:text>12345</xsl:text>-<xsl:value-of select="$pChargeID"/>-->
	</xsl:template>
</xsl:stylesheet>
 
It would seem that first you need to find the highest number of PleaEventSequence. It is not clear whether this is over the entire document or specific to a single <Charge>.

Presuming you are in the context of a single Charge element you can do something like this (typed, not tested):
Code:
<xsl:variable name="myHistoryID">
[indent]<xsl:for-each select="ChargeHistory">[/indent]
[indent][indent]<xsl:sort select="@PleaEventSequence" data-type="number" order="descending"/>[/indent][/indent]
[indent][indent]<xsl:if test="position() = 1"><xsl:value-of select="@InternalOffenseHistoryID"/></xsl:if>[/indent][/indent]
[indent]</xsl:for-each>[/indent]
</xsl:variable>

You could also use recursion, but that is a bit more complex and for this situation I would use <xsl:sort>

Then, the XPath expression that returns the corresponding <Plea> node is:
Code:
//Plea[@InternalOffenseHistoryID = $myHistoryID]

So, putting this all together in an example that would iterate through all the charges, you would have something like this:
Code:
<xsl:for-each select="//Charge">
[indent]<xsl:variable name="myHistoryID">[/indent]
[indent][indent]<xsl:for-each select="ChargeHistory">[/indent][/indent]
[indent][indent][indent]<xsl:sort select="@PleaEventSequence" data-type="number" order="descending"/>[/indent][/indent][/indent]
[indent][indent][indent]<xsl:if test="position() = 1"><xsl:value-of select="@InternalOffenseHistoryID"/></xsl:if>[/indent][/indent][/indent]
[indent][indent]</xsl:for-each>[/indent][/indent]
[indent]</xsl:variable>[/indent] 
[indent]<xsl:for-each select="//Plea[@InternalOffenseHistoryID = $myHistoryID]">[/indent]
[indent][indent] <!-- we are now in the context of the selected Plea element --> [/indent][/indent]
[indent][indent]<PleaText><xsl:value-of select="PleaType"/></PleaText>[/indent][/indent]
[indent]</xsl:for-each>[/indent]
</xsl:for-each>

I hope this gives you a correct idea of how to find your desired Plea element...

Tom Morrison



 
This was helpful Tom. However I think my description was not clear. The result I got was Guilty. However I wanted to get the ChargeID instead.

Please not there could be more than one PleaEvent and each PleaEvent can have more than one ChargeHistory.

Here is a better description of what I am trying to do.

What I am trying to do is get the ChargeID=10485838 . To get this chargeID, I need to look in the ChargeHistory element for the Stage="Plea Event" and find the PleaEventSequence value with the highest event number (for example PleaEventSequence="2") and then find the Plea in the PleaEvent node whose ChargeHistoryID matches the Charge node's ChargeHistoryID . In my case this would be ChargeHistoryID="41274044".
Someone please help me with his one.
 

momo2000 said:
Someone please help me with his one.
A bit of a non sequitur.

So let's take this one step at a time. because there is still ambiguity in your problem statement.

momo2000 said:
To get this chargeID, I need to look in the ChargeHistory element for the Stage="Plea Event"
Any ChargeHistory element? Is there a guarantee that there will be only one element with a 'highest' value for PleaEventSequence? Looking at the posted download XML document, this is indeed the case, but will it always be the case?
 
Yes it is the case that there will always be only one element with a highest value for PleaEventSequence.
 
I have resolved this by adding 2 variables in my xslt code. Then I stored InternalOffenseHistoryID for the Charge and for the PleaEvent.

Code:
<xsl:variable name="vInternalOffenseHistoryID">
	<xsl:value-of select="//Charge[@ID=$pChargeID]/ChargeHistory[@Stage='Plea Event'][last()]/@InternalOffenseHistoryID"/>
	</xsl:variable>
     <xsl:variable name="vPleaEventID">
	<xsl:value-of select="//PleaEvent[count(Plea[@InternalOffenseHistoryID=$vInternalOffenseHistoryID])>0]/@ID"/>
	</xsl:variable>
	<!--Return the selected PleaEventID-->
	<xsl:value-of select="$vPleaEventID"/>
 
This may be unreliable, depending upon how the document is serialized by the generating application. The fact that you are using the predicate [tt][last()][/tt] depends upon the fact that the desired ChargeHistory node is always the last ChargeHistory node in document order. It is probably better not to rely on document order for such a complex document, when a more reliable mechanism is available.

For your first variable, perhaps this:
Code:
<xsl:variable name="vInternalOffenseHistoryID">
[indent]<xsl:for-each select="//Charge[@ID=$pChargeID]/ChargeHistory[@Stage='Plea Event']">[/indent]
[indent][indent]<xsl:sort select="@PleaEventSequence" data-type="number" order="descending"/>[/indent][/indent]
[indent][indent]<xsl:if test="position() = 1"><xsl:value-of select="@InternalOffenseHistoryID"/></xsl:if>[/indent][/indent]
[indent]</xsl:for-each>[/indent]
</xsl:variable>

This will assure you that you are indeed getting the node that has the highest PleaEventSequence numeric value, without regard to document order. The nodes that are selected by the for-each are sorted on the value of the PleaEventSequence attribute in descending numeric order, and then the <xsl:if> allows the value of the first node (the one with the highest value) to participate in setting the value of the variable.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top