xsl if test for no child element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grbeal
    New Member
    • Feb 2008
    • 4

    xsl if test for no child element

    How do I test for a child element with xsl if condition?

    We have a vendor application that outputs an XML file containing records of School Closings due to inclement weather. That XML file gets FTP'd to my web host when the Access database is changed. I'm using Dreamweaver to create an XSLT fragment to read the XML file and include the HTML output into my ASP page. It works fine to display the XML data, the School Closings, in my web page.

    However, when there are no School Closings (weather is nice), the XML that is output contains only a root element and no child nodes or tags inside the root. I'd like to setup an xsl if (or something) that would output the HTML text "There are no School Closings today.", when this XML file is empty.

    I've fumbled around with several different xsl code bits I've googled on the web, but nothing seems to work. I appreciate any help, its my first experience with XML.

    Here's the code from my Dreamweaver created XSLT fragment. Where would I put the xsl if test?

    <?xml version="1.0" encoding="iso-8859-1"?><!-- DWXMLSource="ht tp://www.wbcl.org/cgsclosing/webclose.xml" -->
    <!DOCTYPE xsl:stylesheet [
    <!ENTITY nbsp "&#160;">
    <!ENTITY copy "&#169;">
    <!ENTITY reg "&#174;">
    <!ENTITY trade "™">
    <!ENTITY mdash "—">
    <!ENTITY ldquo "“">
    <!ENTITY rdquo "”">
    <!ENTITY pound "&#163;">
    <!ENTITY yen "&#165;">
    <!ENTITY euro "€">
    ]>
    <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="iso-8859-1"/>
    <xsl:template match="/">

    <xsl:for-each select="WEBCLOS E_XML/ORGANIZATION">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" >
    <tr>
    <td colspan="2">Sta te:&nbsp;<xsl:v alue-of select="STATE"/>&nbsp;&nbsp;&n bsp;City:&nbsp; <xsl:value-of select="CITY"/></td>
    </tr>
    <tr>
    <td colspan="2">&nb sp;&nbsp;&nbsp; &nbsp;<strong>< xsl:value-of select="ORG"/></strong></td>
    </tr>
    <tr>
    <td width="12%" align="right">S tatus:&nbsp;</td>
    <td width="88%"><xs l:value-of select="STATUS"/></td>
    </tr>
    <tr>
    <td align="right">M ore Info.:&nbsp;</td>
    <td><xsl:valu e-of select="STATUS2 "/></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    I'm guessing no closings means no "WEBCLOSE_X ML/ORGANIZATION" nodes?
    Add an <xsl:if> at the beginning:
    [code=xml]
    <xsl:if test="not(WEBCL OSE_XML/ORGANIZATION)">
    <xsl:text> No schools closed today </xsl:text>
    </xsl:if>
    [/code]

    Comment

    • grbeal
      New Member
      • Feb 2008
      • 4

      #3
      Thanks jkmyoung,

      That has worked great. I have another question if you could help me out.

      I also want to create another report from the XML file that will show all records that were entered after Noon. I've got this element in the XML file, here's an example: <ENTERED_AT>08: 30:25 AM</ENTERED_AT>. How do I code the 'xsl if' to test for time?

      This doesn't seem to work: ENTERED_AT > 12. There must be some way to declare the ENTERED_AT element as a time value?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Maybe:
        "substring-after(ENTERED_A T,' ') = 'PM'"

        Comment

        • grbeal
          New Member
          • Feb 2008
          • 4

          #5
          Originally posted by jkmyoung
          Maybe:
          "substring-after(ENTERED_A T,' ') = 'PM'"
          Dude, that did it. Thank you, Thank you, Thank you.

          Comment

          Working...