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 skip all child elements of a node if one specific element has no value? 1

Status
Not open for further replies.

momo2000

Programmer
Jan 2, 2015
63
US
I would like to Check if VehicleLicensePlateNumber in xml document has a value.

In my xslt this is presented as nc:IdentificationID.
If VehicleLicensePlateNumber has a value, then I want to display it and all child elements of j:ConveyanceRegistrationPlateIdentification in xslt code.

If the VehicleLicensePlateNumber has no value in xml document then I want to skip all elements in j:ConveyanceRegistrationPlateIdentification.

I need to do this because sometimes an xml document may not have a VehicleLicensePlateNumber and for that reason the document fails the schema validation.

How do I do this?

Sample xml document

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="IXML Case Notification Test" MessageID="67077798" xmlns="">
  <Case Op="E" InternalID="1617004658" ID="12007544" xmlns:user="[URL unfurl="true"]http://tylertechnologies.com">[/URL]
    <Charge ID="10781361" PartyID="12290304" InternalChargeID="1616630646" InternalPartyID="704622831" xmlns:reslib="urn:reslib">
        <ChargeOffenseDate>06/29/2014</ChargeOffenseDate>
        <Vehicle>
            <VehicleLicensePlateNumber>912KUZ</VehicleLicensePlateNumber>
        </Vehicle>
    </Charge>
  </Case>
</Integration>

xslt code
Code:
 <!--Charge Details Template-->
<xsl:template name="ChargeDetails">
    <ext:Vehicle>
        <j:ConveyanceRegistrationPlateIdentification>
            <xsl:choose>
                <xsl:when test="/Vehicle/VehicleLicensePlateNumber">
            <nc:IdentificationID>
                <xsl:value-of select="/Vehicle/VehicleLicensePlateNumber"/>
            </nc:IdentificationID>
                </xsl:when>
                <xsl:otherwise>
                    <nc:IdentificationID>
                        <xsl:value-of select="/Integration/Citation/Vehicle/VehicleLicensePlateNumber"/>
                    </nc:IdentificationID>
                </xsl:otherwise>
            </xsl:choose>
            <nc:IdentificationJurisdiction>
                <!--Check if PlateState is US or Canadian-->
                <xsl:variable name="vVehicleLicensePlateState">
                    <xsl:value-of select="../Vehicle/VehicleLicensePlateState"/>
                </xsl:variable>
                <xsl:variable name="vUsVehicleLicensePlateStateCode" select="document(concat($gEnvPath,'\Schemas\NiemExchanges\DvsDriverLicenseNotification\niem\codes\usps_states\3.0\1\usps_states.xsd'))/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=$vVehicleLicensePlateState]/@value"/>
                <xsl:choose>
                    <xsl:when test="string-length($vUsVehicleLicensePlateStateCode)>0">
                        <nc-3.0.1:JurisdictionUSPostalServiceCode>
                            <xsl:value-of select="$vUsVehicleLicensePlateStateCode"/>
                        </nc-3.0.1:JurisdictionUSPostalServiceCode>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:variable name="vCanadianVehicleLicensePlateState">
                            <xsl:value-of select="../Vehicle/VehicleLicensePlateState"/>
                        </xsl:variable>
                        <xsl:variable name="vCanadianVehicleLicensePlateStateCode" select="document(concat($gEnvPath,'\Schemas\NiemExchanges\DvsDriverLicenseNotification\niem\codes\canada_post\3.0\post-canada.xsd'))/xs:schema/xs:simpleType/xs:restriction/xs:enumeration[@value=$vCanadianVehicleLicensePlateState]/@value"/>
                        <xsl:choose>
                            <xsl:when test="string-length($vCanadianVehicleLicensePlateStateCode)>0">
                                <nc:JurisdictionCanadianProvinceCode>
                                    <xsl:value-of select="$vCanadianVehicleLicensePlateStateCode"/>
                                </nc:JurisdictionCanadianProvinceCode>
                            </xsl:when>
                            <xsl:otherwise>
                                <nc:JurisdictionText>
                                    <xsl:value-of select="/Integration/Citation/Agency/@Word"/>
                                </nc:JurisdictionText>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </nc:IdentificationJurisdiction>
        </j:ConveyanceRegistrationPlateIdentification>
    </ext:Vehicle>
</xsl:template>
 
Often the best way to do this is make the determination using an variable (<xsl:variable>) immediately inside the template. This variable can then be used to control the conditional parts of the output generation (xsl:if or xsl:choose).

Give it a try. I think you are on your way with what you are already showing.

Tom Morrison
Hill Country Software
 
Something like this, perhaps?
Code:
<!--Charge Details Template-->
<xsl:template name="ChargeDetails">
<xsl:variable name="thePlate"><xsl:choose>
                              <xsl:when test="/Vehicle/VehicleLicensePlateNumber"><xsl:value-of select="/Vehicle/VehicleLicensePlateNumber"/></xsl:when>
			      <xsl:when test="/Integration/Citation/Vehicle/VehicleLicensePlateNumber"><xsl:value-of select="/Integration/Citation/Vehicle/VehicleLicensePlateNumber"/></xsl:when>
			      </xsl:otherwise></xsl:choose></xsl:variable>
<xsl:variable name="hasPlate"><xsl:choose><xsl:when test="$thePlate">T</xsl:when><xsl:otherwise>F</xsl:otherwise></xsl:choose></xsl:variable>

    <ext:Vehicle>
    <xsl:if test="$hasPlate = 'T'">
        <j:ConveyanceRegistrationPlateIdentification>
	    <nc:IdentificationID><xsl:value-of select="$thePlate"/></nc:IdentificationID>
            <nc:IdentificationJurisdiction>
                <!--Check if PlateState is US or Canadian-->
	...
            </nc:IdentificationJurisdiction>
        </j:ConveyanceRegistrationPlateIdentification>
    </xsl:if>
    </ext:Vehicle>
</xsl:template>

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

Part and Inventory Search

Sponsor

Back
Top