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 output all charges with with Pending ID?

Status
Not open for further replies.

momo2000

Programmer
Jan 2, 2015
63
US
I would like to output all Charge elements for all Charges, when OffenseID/ID ID=PENDING.
The important thing to note is that the element <OffenseCite>xyz</OffenseCite will be output as <StatuteNumber>xyz</StatuteNumber>

Sample Output for each Charge should look like this. I did not include all elements
XML:
<Charge>
<ReportingAgency>xyz</ReportingAgency>
<Statute>
<StatuteDescription>TRAFFIC - DWI - OPERATE MOTOR VEHICLE - ALCOHOL CONCENTRATION 0.08 WITHIN 2 HOURS</StatuteDescription>
<StatuteNumber>169A.20.1(5)</StatuteNumber>
</Statute>
</Charge>

Input xml doc

XML:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns:NS1="[URL unfurl="true"]http://www.w3.org/2003/05/soap-envelope">[/URL]
	<Body>
		<TabChargeDocument>

			<Charge>
				<ChargeSequenceID>
					<ID>1</ID>
				</ChargeSequenceID>
				<OffenseID>
					<ID>PENDING</ID>
				</OffenseID>
				<OffenseCite>169A.27.1</OffenseCite>
				<OffenseDescriptionText>TRAFFIC - DWI - FOURTH-DEGREE DRIVING WHILE IMPAIRED; DESCRIBED</OffenseDescriptionText>
				<OffenseDate>2016-09-18</OffenseDate>
				<OffenseTime>00:22:00</OffenseTime>
				<OffenseDatesOnOrAbout>false</OffenseDatesOnOrAbout>
				<SeverityLevelText>Misdemeanor</SeverityLevelText>
				<MinnesotaOffenseCode>NBR</MinnesotaOffenseCode>
				<Report>
					<ReportingAgencyORI>
						<ID>MNMHP0400</ID>
					</ReportingAgencyORI>
					<ReportingAgencyControlNumber>
						<ID>16510299</ID>
					</ReportingAgencyControlNumber>
					<ReportingOfficial>
						<OfficerBadgeNumber>
							<ID>176</ID>
						</OfficerBadgeNumber>
						<OfficerNameText>UTES</OfficerNameText>
					</ReportingOfficial>
				</Report>
				<CommunityOfOffenseText>Brooklyn Park</CommunityOfOffenseText>
			</Charge>
			<Charge>
				<ChargeSequenceID>
					<ID>2</ID>
				</ChargeSequenceID>
				<OffenseID>
					<ID>PENDING</ID>
				</OffenseID>
				<OffenseCite>169A.20.1(5)</OffenseCite>
				<OffenseDescriptionText>TRAFFIC - DWI - OPERATE MOTOR VEHICLE - ALCOHOL CONCENTRATION 0.08 WITHIN 2 HOURS</OffenseDescriptionText>
				<OffenseDate>2016-09-18</OffenseDate>
				<OffenseTime>00:22:00</OffenseTime>
				<OffenseDatesOnOrAbout>false</OffenseDatesOnOrAbout>
				<SeverityLevelText>Misdemeanor</SeverityLevelText>
				<Report>
					<ReportingAgencyORI>
						<ID>MNMHP0400</ID>
					</ReportingAgencyORI>
					<ReportingAgencyControlNumber>
						<ID>16510299</ID>
					</ReportingAgencyControlNumber>
					<ReportingOfficial>
						<OfficerBadgeNumber>
							<ID>176</ID>
						</OfficerBadgeNumber>
						<OfficerNameText>UTES</OfficerNameText>
					</ReportingOfficial>
				</Report>
				<CommunityOfOffenseText>Brooklyn Park</CommunityOfOffenseText>
			</Charge>
			<Charge>
				<ChargeSequenceID>
					<ID>3</ID>
				</ChargeSequenceID>
				<OffenseID>
					<ID>PENDING</ID>/Envelope/Body/TabChargeDocument/Charge[3]/OffenseID/ID
				</OffenseID>
				<OffenseCite>169A.20.1(1)</OffenseCite>
				<OffenseDescriptionText>TRAFFIC - DWI - OPERATE MOTOR VEHICLE UNDER INFLUENCE OF ALCOHOL</OffenseDescriptionText>
				<OffenseDate>2016-09-18</OffenseDate>
				<OffenseTime>00:22:00</OffenseTime>
				<OffenseDatesOnOrAbout>false</OffenseDatesOnOrAbout>
				<SeverityLevelText>Misdemeanor</SeverityLevelText>
				<Report>
					<ReportingAgencyORI>
						<ID>MNMHP0400</ID>
					</ReportingAgencyORI>
					<ReportingAgencyControlNumber>
						<ID>16510299</ID>
					</ReportingAgencyControlNumber>
					<ReportingOfficial>
						<OfficerBadgeNumber>
							<ID>176</ID>
						</OfficerBadgeNumber>
						<OfficerNameText>UTES</OfficerNameText>
					</ReportingOfficial>
				</Report>
				<CommunityOfOffenseText>Brooklyn Park</CommunityOfOffenseText>
			</Charge>
		</TabChargeDocument>
		</Body>
</Envelope>
 
Without an attempt at an XSL, it is unclear what problem you may be having.

Hints:

Selecting the appropriate Charge elements XPath expression might look something like this:
Code:
//TabChargeDocument/Charge[OffenseID/ID = 'PENDING']
Typed, not tested. Your actual input document probably has more XML namespaces than you have shown. If so, you will have to add the namespace alias references to the various XPath elements.

The appropriate technique to use when copying a subtree from the input document to the output document is an identity transform. Using an identity transform, you can then add specific templates to override the copying. In this case you would like to rename a specific element. You can find an example here.

Tom Morrison
Hill Country Software
 
Taking on Tom Morrison's indications:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">

  <xsl:output method="xml"/>

  <xsl:template match="TabChargeDocument">
    <xsl:copy>
      <xsl:apply-templates select="Charge[OffenseID/ID = 'PENDING']"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Charge[OffenseID/ID = 'PENDING']">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="OffenseCite">
    <xsl:element name="StatuteNumber">
      <xsl:copy-of select="text()"/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
 
Close, but...

I would suggest a template that matches "/" and from there do an apply-templates that selects "//TabChargeDocument/Charge[OffenseID/ID = 'PENDING']". This will intercept the natural progression of the built-in rules and 'skip over' the SOAP <envelope> and <body> elements. If you don't do this, the combination of the identity transform and built-in rules will recurse through them, and copy them to the output document.

I also prefer an apply-templates within or at the end of each template that matches the special elements. This may not be necessary in these particular cases (where the elements appear to be leaf nodes in the input tree), but in general you have to restart the recursion down the document tree.


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

Part and Inventory Search

Sponsor

Back
Top