xsl-list
[Top] [All Lists]

Re: HOW TO CHECK FOR EXISTENCE OF A NODE

2003-03-14 11:07:00
Mike,

You should really consider purchasing a book or two on XSLT.  This type of a
question can easily be answered there.  Michael Kay's XSLT or Jeni
Tennison's Learning XSLT are great references.  In addition, please look
through http://www.dpawson.co.uk/.  Jeni also has a nice website that has a
broad introduction to some of the questions you'll probably come up with:
http://www.jenitennison.com/xslt/.  Finally, I think you should take a look
at the <xsl:if> and <xsl:choose><xsl: when/><xsl:otherwise/></xsl:choose>
elements.  I believe you can get away with using the <if> element, but you
may have other cases.  The choose/when/otherwise acts in a similar manner to
the if-then-else statements in c.

I made the assumption that you have an XSLT processor capable of handling
version 1.1 of XSLT.  In addition, I made the assumption that you did not
want a box to appear at all unless the current node contained the
"header_notes" element.  I've also included a full stylesheet with a variety
of different elements from within the XSLT language to give you and idea of
what you should be able to learn how to do from the references I mentioned
above.


<x:stylesheet version="1.1" xmlns:x="http://www.w3.org/1999/XSL/Transform";>

    <x:attribute-set name="box">
        <x:attribute name="border">4</x:attribute>
        <x:attribute name="width">100%</x:attribute>
        <x:attribute name="valign">top</x:attribute>
        <x:attribute name="cellpadding">5</x:attribute>
    </x:attribute-set>

    <x:attribute-set name="cell">
        <x:attribute name="colspan">2</x:attribute>
        <x:attribute name="width">100%</x:attribute>
        <x:attribute name="valign">top</x:attribute>
    </x:attribute-set>

    <x:template match="/">

        <!-- Do something here to get it into the right form -->

        <x:if test="header_notes">
            <x:call-template name="makeBox">
                <x:with-param name="node" select="."/>
            </x:call-template>
        </x:if>

    </x:template>


    <x:template name="makeBox">
        <x:param name="node" select="."/>

        <x:element name="table" use-attribute-sets="box">
            <x:element name="tr">
                <x:element name="td" use-attribute-sets="cell">
                    <x:for-each select="$node/header_notes">
                        <x:value-of select="header_notes_text"/><x:element
name="br"/>
                    </x:for-each>
                </x:element>
            </x:element>
        </x:element>

    </x:template>


</x:stylesheet>

----- Original Message -----
From: "Mike Rudolph" <mrudolph(_at_)gbp(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Friday, March 14, 2003 10:54 AM
Subject: [xsl] HOW TO CHECK FOR EXISTENCE OF A NODE



Newbie here.  I am looking for the proper element to evaluate the
existence
of a node in order to suppress the generation of an HTML display 'box' if
a
particular node does not exist.  So, instead of producing a box with a
caption of 'HEADER NOTES' when there are no header notes, I would rather
not
produce the box at all to cut down on the clutter.  Here is the code which
makes the box and displays any header_notes_text if it exists.   For
example,


     <TABLE BORDER="4" WIDTH="100%" VALIGN="TOP" CELLPADDING="5">
       <TR>
         <TD WIDTH="100%" VALIGN="TOP" COLSPAN="2">
           <H4 ALIGN="LEFT">HEADER NOTES AREA</H4>
              <xsl:for-each select="header_notes">
              <xsl:value-of select="header_notes_text" /><BR/>
              </xsl:for-each>
         </TD>
       </TR>
     </TABLE>



I tried wrapping the above with xsl:template but got this error:
Keyword xsl:template may not be used here.

I have not had any ah-ha experiences yet, so I don't see the obvious.

----------

Mike Rudolph - EDI Coordinator
Green Bay Packaging Inc.
Phone: 920.433.5426
Fax:   920.438.5426
Email: mrudolph(_at_)gbp(_dot_)com


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>