none
How do I check the first 3 characters in an element? RRS feed

  • Question

  • I have the node below. I would like to check if the first 3 characters are not equal to 169.

    How do I do it using xslt if test? e.g. substring(StatuteCode,1,4)="169") I think is is wrong.

    <StatuteCode Word="169142a4">Running</StatuteCode>

    my code

    <xsl:choose>
    	<xsl:when test='(StatuteCode,1,4)="169")'>
    		<xsl:value-of select="true()"/>
    	</xsl:when>
    	<xsl:otherwise>
    		<xsl:value-of select="false()"/>
    	</xsl:otherwise>
    </xsl:choose>


    • Edited by winkimjr2 Tuesday, August 7, 2018 4:56 PM Added what I have for xslt code
    Tuesday, August 7, 2018 4:42 PM

All replies

  • Hi there,

    XML:

    <?xml version="1.0"?>
    <StatuteCode Word="169142a4">Running</StatuteCode>

    XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
    	<xsl:template match="/StatuteCode">
    		<root>
    			<xsl:choose>
    				<xsl:when test="starts-with(@Word,'169')">
    					<xsl:value-of select="true()"/>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:value-of select="false()"/>
    				</xsl:otherwise>
    			</xsl:choose>
    		</root>
    	</xsl:template>
    </xsl:stylesheet>

    Output:

    <root>true</root>

    Tuesday, August 7, 2018 5:21 PM
  • Thanks for your help. It is now working.
    Tuesday, August 7, 2018 5:37 PM
  • Hi winkimjr2,

    Glad to hear that it is working for you.

    Please don't forget to mark the post as answered.

    Tuesday, August 7, 2018 5:44 PM
  • What would be the path for checking if the first 3 characters are 169 for the StatuteCode node's Word?

    <Charge>
       <ChargeHistory>
    	 <Statute>
    	   <StatuteCode Word="269142a4">Traffic</StatuteCode>
    		</Statute>
       </ChargeHistory>
    </Charge>


    • Edited by winkimjr2 Tuesday, August 7, 2018 6:08 PM Added a StatuteNode
    Tuesday, August 7, 2018 6:07 PM
  • XML:

    <?xml version="1.0"?>
    <Charge>
    	<ChargeHistory>
    		<Statute>
    			<StatuteCode Word="269142a4">Traffic</StatuteCode>
    		</Statute>
    	</ChargeHistory>
    </Charge>


    XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
    	<xsl:template match="/Charge/ChargeHistory/Statute/StatuteCode">
    		<root>
    			<xsl:choose>
    				<xsl:when test="starts-with(@Word,'169')">
    					<xsl:value-of select="true()"/>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:value-of select="false()"/>
    				</xsl:otherwise>
    			</xsl:choose>
    		</root>
    	</xsl:template>
    </xsl:stylesheet>

    Output:

    <root>false</root>

    Tuesday, August 7, 2018 6:12 PM
  • I am sorry. I meant how to check that the first 3 characters for StatuteCode word are not 169.
    <StatuteCode Word="269142a4">Traffic</StatuteCode>
    Tuesday, August 7, 2018 6:16 PM
  • XSLT:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    
    	<xsl:template match="/Charge/ChargeHistory/Statute/StatuteCode">
    		<root>
    			<xsl:choose>
    				<xsl:when test="not(starts-with(@Word,'169'))">
    					<xsl:value-of select="true()"/>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:value-of select="false()"/>
    				</xsl:otherwise>
    			</xsl:choose>
    		</root>
    	</xsl:template>
    </xsl:stylesheet>

    Tuesday, August 7, 2018 6:18 PM