none
How Can i add a wild card condition for XSLT choose RRS feed

  • Question

  • Currently  i have Requirement to add a wildcard condition for a Choose statement condition, i want to filter out and equal first two digit start from  'US' ,  Discharge and LoadPort tags. Below is a my own created script but its not functioninonfg, Could you  please guide me to correct this,

    <xsl:for-each select="a:Root/Consol_Details">
      
    <xsl:choose>
        <xsl:when test="position() = 2  and   Discharge = 'US*' ">   
     
    <RecipientRoleCollection>
            <RecipientRole> 
          <Code>RAG</Code>
      </RecipientRole>
     </RecipientRoleCollection>
      
        </xsl:when>
        <xsl:when test="position() = 2  and   LoadPort = 'US*' ">   
         
    <RecipientRoleCollection>
            <RecipientRole> 
          <Code>SAG</Code>
      </RecipientRole>
     </RecipientRoleCollection>
      
        </xsl:when>
      </xsl:choose>
      
    </xsl:for-each>

    Wednesday, September 4, 2019 5:04 AM

All replies

  • Did you try this: “position() = 2 and starts-with(Discharge, 'US')”?

    See: https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256174(v=vs.100).

    • Edited by Viorel_MVP Wednesday, September 4, 2019 5:41 AM
    Wednesday, September 4, 2019 5:40 AM
  • Hi Maheshma,

    A usual XSLT question should contain 3 explicit artifacts:

    1. Input XML;
    2. XSLT and logic explanation;
    3. Desired output;

    So I took the liberty and created the following.

    Input XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Root xmlns="http://IntegrationAccount1.Schema16">
    	<Consol_Details xmlns="">
    		<ShipmentNo>Shipement_No</ShipmentNo>
    		<ConsolType>Type</ConsolType>
    		<TransportMode>Transport</TransportMode>
    		<ContainerMode>Container Mode</ContainerMode>
    		<Load1st>1st Load</Load1st>
    		<Discharge>INMAA</Discharge>
    	</Consol_Details>
    	<Consol_Details xmlns="">
    		<ShipmentNo>S10219089768</ShipmentNo>
    		<ConsolType>AGT</ConsolType>
    		<TransportMode>SEA</TransportMode>
    		<ContainerMode>FCL</ContainerMode>
    		<Load1st>LKCMB</Load1st>
    		<Discharge>USJFK</Discharge>
    	</Consol_Details>
    	<Consol_Details xmlns="">
    		<ShipmentNo>S10219089769</ShipmentNo>
    		<ConsolType></ConsolType>
    		<TransportMode></TransportMode>
    		<ContainerMode></ContainerMode>
    		<Load1st></Load1st>
    		<Discharge></Discharge>
    	</Consol_Details>
    </Root>

    XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://IntegrationAccount1.Schema16" exclude-result-prefixes="a">
    	<xsl:output indent="yes" method="xml" encoding="utf-8"/>
    
    	<xsl:template match="/a:Root">
    		<UniversalShipment xmlns="http://www.wise.lk/Schemas/Universal/2011/11" version="1.1">
    			<xsl:for-each select="Consol_Details">
    				<row>
    					<ShipmentNo>
    						<xsl:value-of select="ShipmentNo"/>
    					</ShipmentNo>
    					<ConsolType>
    						<xsl:value-of select="ConsolType"/>
    					</ConsolType>
    					<TransportMode>
    						<xsl:value-of select="TransportMode"/>
    					</TransportMode>
    					<xsl:choose>
    						<xsl:when test="position() = 2 and starts-with(Discharge,'US')">
    							<RecipientRoleCollection>
    								<RecipientRole>
    									<Code>RAG</Code>
    								</RecipientRole>
    							</RecipientRoleCollection>
    						</xsl:when>
    						<xsl:when test="position() = 1 and starts-with(Discharge,'IN')">
    							<RecipientRoleCollection>
    								<RecipientRole>
    									<Code>SAG</Code>
    								</RecipientRole>
    							</RecipientRoleCollection>
    						</xsl:when>
    						<xsl:otherwise>
    							<notFound>blank</notFound>
    						</xsl:otherwise>
    					</xsl:choose>
    				</row>
    			</xsl:for-each>
    		</UniversalShipment>
    	</xsl:template>
    </xsl:stylesheet>

    Output:
    <?xml version="1.0" encoding="utf-8"?>
    <UniversalShipment version="1.1" xmlns="http://www.wise.lk/Schemas/Universal/2011/11">
      <row>
        <ShipmentNo>Shipement_No</ShipmentNo>
        <ConsolType>Type</ConsolType>
        <TransportMode>Transport</TransportMode>
        <RecipientRoleCollection>
          <RecipientRole>
            <Code>SAG</Code>
          </RecipientRole>
        </RecipientRoleCollection>
      </row>
      <row>
        <ShipmentNo>S10219089768</ShipmentNo>
        <ConsolType>AGT</ConsolType>
        <TransportMode>SEA</TransportMode>
        <RecipientRoleCollection>
          <RecipientRole>
            <Code>RAG</Code>
          </RecipientRole>
        </RecipientRoleCollection>
      </row>
      <row>
        <ShipmentNo>S10219089769</ShipmentNo>
        <ConsolType></ConsolType>
        <TransportMode></TransportMode>
        <notFound>blank</notFound>
      </row>
    </UniversalShipment>



    Wednesday, September 4, 2019 1:08 PM