INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How do I skip all child elements of a node if one specific element has no value?

How do I skip all child elements of a node if one specific element has no value?

How do I skip all child elements of a node if one specific element has no value?

(OP)
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

CODE --> xml

<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="IXML Case Notification Test" MessageID="67077798" xmlns="">
  <Case Op="E" InternalID="1617004658" ID="12007544" xmlns:user="http://tylertechnologies.com">
    <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 --> xslt

<!--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> 

RE: How do I skip all child elements of a node if one specific element has no value?

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

RE: How do I skip all child elements of a node if one specific element has no value?

Something like this, perhaps?

CODE --> XSLT

<!--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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close