Web Analytics Made Easy -
StatCounter xsl if node has value - CodingForum

Announcement

Collapse
No announcement yet.

xsl if node has value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • xsl if node has value

    Hello i have a node that may or may not have a value i want to write out that value if it does and not if it doesnt
    here is my page so far i bolded the area in question
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
      <html>
      <head>
      <script language="javascript">
      function termed(){
      alert("This is a Termed Client");
     }
     function hra(){
     alert("This is an HRA Client");
     }
     function fsa(){
     alert("This is an FSA Client");
     }
     function lfsa(){
     alert("This is an LFSA Client");
     }
     </script>
     </head>
      <body>
        <h2>ClientList</h2>
        <table border="1">
        <tr bgcolor="#9acd32">
          <th align="left">Company</th>
          <th align="left">Control</th>
          <th align="left">Company</th>
          <th align="left">Control</th>
        </tr>
        <xsl:for-each select="clientlist/Table/line">
       <tr>
          <xsl:for-each select="Cell">
          <td><a onclick[B]="<xsl:if test="script"><xsl:value-of select="script" /></xsl:if>" [/B] href="{@HRef}">
          <xsl:value-of select="Data"/></a></td>
          </xsl:for-each>
        </tr>
        </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>
    Thanks

  • #2
    Code:
    <a href="{@HRef}">
    <xsl:attribute name="onclick">
    <xsl:if test="script">
    <xsl:value-of select="script"/>
    </xsl:if>
    </xsl:attribute>
    </a>
    Try that. I haven't tested it, and I haven't used xsl:if before.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

    Comment


    • #3
      Worked perfectly
      final
      Code:
        <xsl:for-each select="clientlist/Table/line">
         <tr>
            <xsl:for-each select="Cell">
            <td><a href="{@HRef}">
            <xsl:attribute name="onclick">
            <xsl:if test="script">
            <xsl:value-of select="script" />
            </xsl:if>
            </xsl:attribute>
            <xsl:value-of select="Data"/></a></td>
            </xsl:for-each>
          </tr>
          </xsl:for-each>
      Thanks

      Comment


      • #4
        ok this did not work..

        this only checks to see if the node exists it doesnt check to see if it has a value is there anyway to modify the above code to check to see if the existing node has a value(text inside of it)

        Comment


        • #5
          Could I have some XML data to work with please?
          Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
          I code to specs, and test only in Firefox (unless stated otherwise).

          Comment


          • #6
            <xsl:if test="Node=''">
            well check if the node has a blank value.. here is my current issue though
            Code:
            <xsl:for-each select="clientlist/Table">
            <script language="javascript">
            var control=document.board.pn.value
            Function checknum()
            <xsl:if test="line/Cell/Data/@Type='Number'">
            if (control=="<xsl:value-of select="line/Cell/Data/>">);
            document.href="<xsl:value-of select="line/Cell/@HRef/>";
            </xsl:if>
            return false;
            </script>
            </xsl:for-each>
            Heres is a line of the xml
            Code:
            <Table>
            <line Height="76.5">
                     <Cell StyleID="s32" HRef="site">
                        <Data Type="String">Lutheran Heritage Foundation</Data>
                        <script/>
                     </Cell>
                     <Cell StyleID="s35" HRef="site">
                        <Data Type="Number">63819</Data>
                        <script/>
                     </Cell>
                     <Cell StyleID="s51" HRef="site">
                        <Data Type="String">WYETH (Formerly known as American Home Products)</Data>
                        <script/>
                     </Cell>
                     <Cell StyleID="s35" HRef="http://www.site.com">
                        <Data Type="Number">9300</Data>
                        <script/>
                     </Cell>
                  </line>
            <!--Alot more lines--!>
            </Table>
            im attemppting to write out a large javascript file from the above xml for each line where the number is the data type i want to write a new line of javascript code I am doing a similar loop through the same xml file on this page but it is to create a table I now need the javascript

            Comment


            • #7
              Okay, this is getting a little messier. Are you able to paste or attach all your code/files? Attaching would be best.
              Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
              I code to specs, and test only in Firefox (unless stated otherwise).

              Comment


              • #8
                i think i just fixed that part but.. ..i get no errors when i run the page it loads quite fast, but when i type a number in the box that is well on down the list like in the z's ..it takes somewhere around 3 seconds to run the javascript? I thought this javascript would be loaded before the body of the page.. I actually wrote a version of this javascript before manually with all 500 clients and it runs very fast.. Is this xml related?.. when I view source on the page i cant see the generated HTML.. i can only see the xml sheet... which i dont really want people to see..

                heres my full xsl code..maybe you can see where my error is
                Code:
                <?xml version="1.0" encoding="ISO-8859-1"?>
                <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                  <xsl:template match="/">
                  <html>
                  <head>
                  <script language="javascript">
                  function termed(){
                  alert("This is a Termed Client");
                 }
                 function hra(){
                 alert("This is an HRA Client");
                 }
                 function fsa(){
                 alert("This is an FSA Client");
                 }
                 function lfsa(){
                 alert("This is an LFSA Client");
                 }
                 function benny(){
                 alert("This is a Benny Card Client");
                 }
                 function chase(){
                 alert("This is a Chase Card Client");
                 }
                 function flex(){
                 alert("This is a Flex Card Client");
                 }
                 function bookmark(){
                 return true;
                 }
                 </script>
                [B]<xsl:for-each select="clientlist/Table">
                <script language="javascript">
                function checknum(){
                <xsl:for-each select="line/Cell">
                
                <xsl:if test="Data/@Type='Number'">
                if (document.board.pn.value=="<xsl:value-of select="Data"/>")
                return <xsl:value-of select="script"/>();
                window.location="<xsl:value-of select="@HRef"/>";
                </xsl:if>
                
                </xsl:for-each>
                return false;
                }
                </script>[/B]</xsl:for-each>
                 </head>
                  <body>
                <form name="board" method="post" onsubmit="checknum();">
                <input type="text" name="pn"/><input type="submit"/></form>
                    <h2>ClientList</h2>
                    <table border="1">
                    <tr bgcolor="#9acd32">
                      <th align="left">Company</th>
                      <th align="left">Control</th>
                      <th align="left">Company</th>
                      <th align="left">Control</th>
                    </tr>
                    <xsl:for-each select="clientlist/Table/line">
                   <tr>
                      <xsl:for-each select="Cell">
                      <td>
                       <xsl:if test="NamedCell">
                       <a><xsl:attribute name="name"><xsl:value-of select="NamedCell/@Name"/></xsl:attribute></a>
                       </xsl:if>
                      <xsl:choose>
                      <xsl:when test="Data=''">
                      <xsl:value-of select="Data"/>.
                      </xsl:when>
                      <xsl:otherwise>
                      <xsl:choose>
                      <xsl:when test="script=''">
                      <a href="{@HRef}">
                      <xsl:attribute name="onclick">fsa();</xsl:attribute><xsl:value-of select="Data"/></a>
                      </xsl:when>
                      <xsl:otherwise>
                      <a href="{@HRef}">
                      <xsl:attribute name="onclick"><xsl:value-of select="script"/>();</xsl:attribute><xsl:value-of select="Data"/></a>
                      </xsl:otherwise>
                       </xsl:choose>
                      </xsl:otherwise>
                        </xsl:choose>
                        
                        </td>
                      </xsl:for-each>
                    </tr>
                    </xsl:for-each>
                    </table>
                  </body>
                  </html>
                </xsl:template>
                </xsl:stylesheet>
                Ive attached a copy of the sheet as well
                Attached Files
                Last edited by werD; Aug 3, 2005, 03:38 AM.

                Comment


                • #9
                  It would be much better if you attached both the XSL and XML files.
                  Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
                  I code to specs, and test only in Firefox (unless stated otherwise).

                  Comment


                  • #10
                    ive attched aa portion of the xml to my last post the full file is too large

                    Comment


                    • #11
                      Okay, I've cleaned it up a little, but I'm having a bit of trouble figuring out what you what the script to actually do.

                      Also, your XML is designed incorrectly. You're not getting any benefit over standard HTML. Your XML tells me almost nothing about the data.

                      Give me as much detail as possible, and I'll have another look tomorrow morning.
                      Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
                      I code to specs, and test only in Firefox (unless stated otherwise).

                      Comment


                      • #12
                        ok.. Thanks for all your help so far..

                        The idea of the xml was to make the client list more easily updated by my other staff members. The xml is generated off an excel table and builds the hyperlinks to all our clients.. The rest off the staff wanted something to where they wouldnt have to write in the javascript for new clients or changed clients. How could I do this xml better. This was the qickest way I could generate the xml and quickly manipulate it to make it usable.
                        NamedCell is for letter bookmarks
                        excel table
                        -each line has four cells two data and two string
                        -the data is layed out string,number,string,number
                        -excel table
                        the xml has style for cell class(currently unused in script)
                        and a node in each cell to call the javascript alert.. This way staff only has to type in "fsa" or "termed"
                        What would a better way to write thj xml consist of? I am really new to xml but have been using asp and db recordsets for a while now so my xml logic may be all backwards.

                        Thanks again for your help
                        Last edited by werD; Aug 3, 2005, 12:06 PM.

                        Comment


                        • #13
                          Ok.. Ive looked at this xml a little harder.. I guess excel is just not the way i need it generated.. but I think i can use an xsl to parse the initial xml output a little better. Would this be a more appropriate output?

                          Code:
                          <Table>
                          <line>
                          <Cell Style="style1" HRef="longurl">
                          <Name>Acf Industries</Name>
                          <Control>10029</Control>
                          <Type>fsa</Type>
                          </Cell>
                          <Cell Style="style1" HRef="longurl">
                          <Name>Another Company</Name>
                          <Control>10038</Control>
                          <Type>fsa</Type>
                          </Cell>
                          <line>
                          </Table>
                          Last edited by werD; Aug 3, 2005, 02:15 PM.

                          Comment


                          • #14
                            Can you tell me what the data is though? I should be able to look at the XML and understand what the data is based on the tags around it.

                            You have a clientlist element, but no clients. You have table, line and cell elements, but they're all presentational. What's the relationship between clients that are on the same 'line'? Is the style attribute purely presentational? Does the type element specify the type of company/client? What are the numbers in the last 'cell'? Some sort of internal company/client ID?

                            Try to think about data, structure, style and behaviour as completely seperate layers. Once we've got the data sorted, we'll use XSL to create the structure. After that we use CSS to add the style. Then Javascript to add behaviour. All these are seperate layers, we should be able to apply them without even touching the others.

                            See what I'm getting at?
                            Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
                            I code to specs, and test only in Firefox (unless stated otherwise).

                            Comment


                            • #15
                              I do and Thanks again for your help
                              the clientlist element is only there in case i wanted to add certain opening statements and such i was just trying to be ready for the future
                              The page is laid out in two columns
                              http://12.202.21.191/xml/client/clientlist.xml
                              by alphabet
                              1 2
                              A M
                              B N
                              C O
                              D P
                              E Q
                              F R
                              G S
                              H T
                              I U
                              J V
                              K W
                              L XYZ

                              each line has four cells two of the cells contain info for one client in column 1 the other two are for column 2 the out put is similar to this

                              column 1 column2
                              Co. A #1 Co. M #20


                              The style attribute was intended to change the color of the specific font color or cell background for that hyperlink each script type is color coded with a diff color ie.. FSA is Blue

                              Comment

                              Working...
                              X