Link to home
Start Free TrialLog in
Avatar of Richard Kreidl
Richard KreidlFlag for United States of America

asked on

Checking value of substring in a XML element

I have the following XML element in my XML file:
<TODAY>Thursday, October 13, 2011</TODAY>

In my snippet of XSL code I would like to check the value of that XML element to see if the text "Friday" is present. I'm not sure how to do that either using "when test" or "if test".

<xsl:template match="LASTISA[. != '']">
        <xsl:variable name="time" select="number(concat(substring-before(normalize-space(text()),':'), '.', substring(substring-after(normalize-space(text()),':'),1,2)))"/>   

<!--If the element contains the text "Friday" do the below-->
		 
      <xsl:variable name="AMTime"  
          select="(contains(translate(text(), 'am', 'AM'), 'AM') and ($time &gt; number('12') or $time &lt; number('12')))
            or (contains(translate(text(), 'pm', 'PM'), 'PM') and ($time = number('12')))"/>
        <li>ISA DataBase <font color="#6666ff">(12am SLA)</font> ended at: <font>

<!--If the element doesn't contains the text "Friday" then do this-->

<xsl:variable name="AMTime"  
        select="(contains(translate(text(), 'am', 'AM'), 'AM') and ($time &gt; number('1.0') and $time &lt; number('12')))
            or (contains(translate(text(), 'pm', 'PM'), 'PM') and ($time = number('12')))"/>       
 <li>ISA DataBase <font color="#6666ff">(1am SLA)</font> ended at: <font>

            <xsl:attribute name="color">
            <xsl:choose>
	<xsl:when test="(.='Not completed; See comment above')">#FF0000</xsl:when>
                <xsl:when test="not($AMTime)">#00CC00</xsl:when>
                <xsl:otherwise>#FF6633</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
            <xsl:value-of select="." />
            <xsl:if test="$AMTime"><i><xsl:text>; See comment above</xsl:text></i></xsl:if>
        </font></li>
    </xsl:template>

Open in new window


Thanks
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

<xsl:if test="contains(TODAY, 'Friday')">...
Avatar of Richard Kreidl

ASKER

Not working... here's the updated code:
<xsl:template match="LASTISA[. != '']">
        <xsl:variable name="time" select="number(concat(substring-before(normalize-space(text()),':'), '.', substring(substring-after(normalize-space(text()),':'),1,2)))"/> 
        <xsl:variable name="AMTime"  
            select="(contains(translate(text(), 'am', 'AM'), 'AM') and ($time &gt; number('12') or $time &lt; number('12')))
            or (contains(translate(text(), 'pm', 'PM'), 'PM') and ($time = number('12')))"/>
        [b]<li>ISA DataBase <font color="#6666ff">(12am SLA)</font> ended at: <font>[/b]		 <xsl:attribute name="color">
            <xsl:choose>
			<xsl:when test="(.='Not completed; See comment above')">#FF0000</xsl:when>
                <xsl:when test="not($AMTime)">#00CC00</xsl:when>
                <xsl:otherwise>#FF6633</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
            <xsl:value-of select="." />
            <xsl:if test="$AMTime"><i><xsl:text>; See comment above</xsl:text></i></xsl:if>
        </font></li>
		<xsl:if test="contains(TODAY, 'Friday')">
		 <xsl:variable name="AMTimeF"  
            select="(contains(translate(text(), 'am', 'AM'), 'AM') and ($time &gt; number('1') or $time &lt; number('12')))
            or (contains(translate(text(), 'pm', 'PM'), 'PM') and ($time = number('12')))"/>
        <li>ISA DataBase <font color="#6666ff">(1am SLA)</font> ended at: <font>        
		  <xsl:attribute name="color">
            <xsl:choose>
		<xsl:when test="(.='Not completed; See comment above')">#FF0000</xsl:when>
                <xsl:when test="not($AMTimeF)">#00CC00</xsl:when>
                <xsl:otherwise>#FF6633</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
            <xsl:value-of select="." />
            <xsl:if test="$AMTimeF"><i><xsl:text>; See comment above</xsl:text></i></xsl:if>
        </font></li>
		</xsl:if>
    </xsl:template>

Open in new window


What am I doing wrong?
I tested the XSL with 'Friday' in the XML element:TODAY and the if statement doesn't seem to be working..
Can you send us a sample LASTISA XML?
<?xml version="1.0" standalone="yes"?>
<MSR>
  <Info>
    <AutoSys>None</AutoSys>
    <BT30004>8:04pm</BT30004>
    <CICS />
    <CICST />
    <CM10009 />
    <CMHM />
    <COMP />
    <CSI>CSI outage report will be sent out by the CSI team on Next Business Day.</CSI>
    <Comment />
    <DISTM />
    <DISTS />
    <EBIS />
    <ES00014>2:09am</ES00014>
    <FADS>11:01pm</FADS>
    <FIXA>7:33pm</FIXA>
    <FM24FHR />
    <FMDNFHR />
    <FMI />
    <GDC>9:46pm</GDC>
    <ICCA />
    <ICTM>7:06pm</ICTM>
    <IMZ />
    <IMZT />
    <INSH>8:02pm</INSH>
    <LASTISA>9:15pm</LASTISA>    
<LASTSRM1 />
    <LASTSRM2 />
    <LASTSRM2A />
    <MADYFHR>8:00pm</MADYFHR>
    <MVS>HR80007  IM10919471 also tables ES00024</MVS>
    <NITECOMM />
    <ODSFHR />
    <PAYFHR />
    <PDBN>8:30pm</PDBN>
    <PSF />
    <PSLDFHR />
    <RPI />
    <SALES>5:45am</SALES>
    <SAMS />
    <SIVU />
    <SSEC>11:19pm</SSEC>
    <STMT>CSI outage on Sunday from 12:00am to hh:mm am/pm due to scheduled maintenance.</STMT>
    <TEEFHR />
    <TEPP>7:49pm</TEPP>
    <TNC />
    <TODAY>Friday, October 13, 2011</TODAY>  
  <UMZ />
    <UMZT />
    <UPDATE />
    <maxAutoSys>0</maxAutoSys>
    <numAutoSys>0</numAutoSys>
    <numMVS>0</numMVS>
  </Info>
</MSR>
OK, TODAY is not part of TODAY, it is a sibling, so you will need

        <xsl:if test="contains(../TODAY, 'Friday')">
Now, when I change the TODAY element to: Friday. I get the following two lines in my HTML:

ISA DataBase (12am SLA) ended at: 9:15pm
ISA DataBase (1am SLA) ended at: 9:15pm

I'm only supposed to get the following line:
ISA DataBase (1am SLA) ended at: 9:15pm

Maybe I need to use a "choose" instead of "if"
well, I couldn't possibly know the logic you need if you don't specify it.

If both are mutually exclusive then you need a choose indeed
Basically, the logic is if it's a Friday we want to display this line that has a "1am SLA" and check the time to see if it's before 1am.

ISA DataBase (1am SLA) ended at: 9:15pm

On Mondays through Thursdays we want this line displayed and check to see if the time is before midnight:

ISA DataBase (12am SLA) ended at: 9:15pm


How would I code the choose statement?

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!!