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

How to I check for all Protection orders based on condition? 2

Status
Not open for further replies.

momo2000

Programmer
Jan 2, 2015
63
0
0
US
My template is correctly displaying output when any CasePartyName has an Op='E'.
It is also correctly displaying output when any CaseParty/ObservedRace has an Op='E' or CaseParty/ObeservedEthnicity has an Op='E' and Connection/@Word='RSP'

Current output
XML:
<NotificationEvent notificationType="CasePartyUpdates" >CasePartyUpdate</NotificationEvent>

Now I would like to add an attribute to the above output to display the ProtectionOrder number/s of any ProtectionOrder/s that have Status/Current="true" and Status/Type/@Word="SBJO or SBJOCOR".

Here is what the output based on my xml document should look like..

XML:
<NotificationEvent notificationType="CasePartyUpdates" activeSignedPoNumbers="1605923">CasePartyUpdate</NotificationEvent>

If there were more qualified protectionorders, their numbers would be displayed as activeSignedPoNumber="1605923, 111, 078, 4532" etc.

I am stuck that is why I opted to get help.

My xml document
XML:
<Integration>
	<ControlPoint>SAVE-FAM-CASE</ControlPoint>
	<Case>
		<CaseCategory>FAM</CaseCategory>
		<CaseType Word="DMA">Domestic Abuse</CaseType>
		<CaseParty>
			<ObservedRace Word="W">White</ObservedRace>
			<ObservedEthnicity Word="NH">Non Hispanic</ObservedEthnicity>
			<Connection Word="PET">
			</Connection>
		</CaseParty>
		<CaseParty Op="E">
			<ObservedRace Op="E" Word="M">Multiracial</ObservedRace>
			<ObservedEthnicity Op="E" Word="R">Refused</ObservedEthnicity>
			<Connection Word="RSP">
			</Connection>
		</CaseParty>
		<ProtectionOrders>
			<ProtectionOrder InternalProtectionOrderID="11257">
				<ProtectionOrderNumber>1605921</ProtectionOrderNumber>
				<Statuses>
					<Status>
						<Current>true</Current>
						<Type Word="SUPERSEDED">Superseded</Type>
					</Status>
					<Status>
						<Current>false</Current>
						<Type Word="SBJOCOR">Corrected - Signed By Judicial Officer</Type>
					</Status>
				</Statuses>
			</ProtectionOrder>
			<ProtectionOrder InternalProtectionOrderID="11259">
				<ProtectionOrderNumber>1605923</ProtectionOrderNumber>
				<Statuses>
					<Status>
						<Current>true</Current>
						<Type Word="SBJOCOR">Corrected - Signed By Judicial Officer</Type>
					</Status>
					<Status>
						<Current>false</Current>
						<Type Word="SBJO">Signed By Judicial Officer</Type>
					</Status>
				</Statuses>
			</ProtectionOrder>
		</ProtectionOrders>
	</Case>
</Integration>

Xslt template
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" >
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
			<xsl:call-template name="CasePartyUpdates"/>
	<xsl:template name="CasePartyUpdates">
		<xsl:if test="Integration/ControlPoint='SAVE-FAM-CASE'">
			<xsl:if test="Integration/Case/CaseType/@Word='DMA'">
				<xsl:variable name="vActiveSignedPoNumbers">
					<!--Loop through each PO if current status is SBJO OR SBJOCOR add the PO number -->
				</xsl:variable>
				<xsl:if test="(string-length($vActiveSignedPoNumbers)>0)">
				<xsl:for-each select="Integration/Case">
						<xsl:choose>
							<xsl:when test="(count(CaseParty[((ObservedRace/@Op='E') or (ObservedEthnicity/@Op='E')) and (Connection/@Word='RSP')] )>0)">
								<NotificationEvent notificationType="CasePartyUpdates">
									<xsl:attribute name="activeSignedPoNumbers"><xsl:value-of select="$vActiveSignedPoNumbers"/></xsl:attribute>
									<xsl:text>CasePartyUpdate</xsl:text>
								</NotificationEvent>
							</xsl:when>
							<xsl:when test="(count(CaseParty[CasePartyName/@Op='E']) >0)">
								<NotificationEvent notificationType="CasePartyUpdates">
									<xsl:attribute name="activeSignedPoNumbers"><xsl:value-of select="$vActiveSignedPoNumbers"/></xsl:attribute>
									<xsl:text>CasePartyUpdate</xsl:text>
								</NotificationEvent>
							</xsl:when>
						</xsl:choose>
					</xsl:for-each>
				</xsl:if>
			</xsl:if>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

 
momo2000 said:
Code:
<!--Loop through each PO if current status is SBJO OR SBJOCOR add the PO number -->

Well, I see that you have version="3.0" on your xsl:stylesheet processing instruction, so forgive me for stepping back to good old version 1. I don't seem to have a version 3.0 XSLT processor available.

And in version 1, 'loop through' and 'add [concatenate]' are not terms that go well together. In version 1, one uses recursion.

In the XSL below, the xsl:variable processing instruction invokes a called template with a set of nodes that meet the criteria. The called template then uses recursion to go through all the nodes, building up the desired string.

Code:
<xsl:stylesheet  xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0" >
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
			<xsl:call-template name="CasePartyUpdates"/>
	</xsl:template>
	<xsl:template name="CasePartyUpdates">
		<xsl:if test="Integration/ControlPoint='SAVE-FAM-CASE'">
			<xsl:if test="Integration/Case/CaseType/@Word='DMA'">
				<xsl:variable name="vActiveSignedPoNumbers"><xsl:call-template name="listProtectionOrders">
																<xsl:with-param name="poNodes" select="Integration/Case/ProtectionOrders/ProtectionOrder[Statuses/Status/Type/@Word='SBJO' or Statuses/Status/Type/@Word='SBJOCOR']"/>
															</xsl:call-template></xsl:variable>
				<xsl:if test="(string-length($vActiveSignedPoNumbers)>0)">
				<xsl:for-each select="Integration/Case">
						<xsl:choose>
							<xsl:when test="(count(CaseParty[((ObservedRace/@Op='E') or (ObservedEthnicity/@Op='E')) and (Connection/@Word='RSP')] )>0)">
								<NotificationEvent notificationType="CasePartyUpdates">
									<xsl:attribute name="activeSignedPoNumbers"><xsl:value-of select="$vActiveSignedPoNumbers"/></xsl:attribute>
									<xsl:text>CasePartyUpdate</xsl:text>
								</NotificationEvent>
							</xsl:when>
							<xsl:when test="(count(CaseParty[CasePartyName/@Op='E']) >0)">
								<NotificationEvent notificationType="CasePartyUpdates">
									<xsl:attribute name="activeSignedPoNumbers"><xsl:value-of select="$vActiveSignedPoNumbers"/></xsl:attribute>
									<xsl:text>CasePartyUpdate</xsl:text>
								</NotificationEvent>
							</xsl:when>
						</xsl:choose>
					</xsl:for-each>
				</xsl:if>
			</xsl:if>
		</xsl:if>
	</xsl:template>

	<xsl:template name="listProtectionOrders">
	<xsl:param name="poNodes"/>
	<xsl:param name="poList" select="''"/>
	<xsl:choose>
	<xsl:when test="count($poNodes) = 0"><xsl:value-of select="$poList"/></xsl:when>
	<xsl:otherwise><xsl:call-template name="listProtectionOrders">
						<xsl:with-param name="poNodes" select="$poNodes[position() != 1]"/>
						<xsl:with-param name="poList"><xsl:value-of select="$poList"/><xsl:if test="string-length($poList) != 0">, </xsl:if><xsl:value-of select="$poNodes[1]/ProtectionOrderNumber"/></xsl:with-param>
					</xsl:call-template>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:template>
</xsl:stylesheet>



Tom Morrison
Hill Country Software
 
With a straightforward loop:

Code:
        <xsl:variable name="vActiveSignedPoNumbers">
          <xsl:for-each select="Integration/Case/ProtectionOrders/ProtectionOrder[Statuses/Status[Current = 'true' and (Type/@Word = 'SBJO' or Type/@Word = 'SBJOCOR')]]">
            <xsl:if test="position() &gt; 1"><xsl:text>, </xsl:text></xsl:if>
            <xsl:value-of select="ProtectionOrderNumber/text()"/>
          </xsl:for-each>
        </xsl:variable>
 
atlopes,

Yes, in this situation a loop will work, but for the more general case recursion is the path to a solution. So, I guess because I feel comfortable with a recursion-based solution, I don't look for the special situations (such as this one) where a for-each will indeed work.

So, momo2000, you have two possibilities.

Tom Morrison
Hill Country Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top