xsl-list
[Top] [All Lists]

newbie problem

2005-09-20 05:43:50
Hi
I am very new to xslt and I have tried to find the aswer to my problem
in the FAQs and archives but I suppose I don't really know what i'm
looking for!

An example xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MEASURE>
<MEASURE TITLE="Test measure elements">
        <BOOLEAN NAME="boolelement" CAPTION="Is it?" CHECKED="yes" />
        <CHOICE NAME="choiceelement" CAPTION="Which one?" >
               <CHOICE_ELEMENT VALUE="0" CAPTION="This one" CHECKED="yes" />
                <CHOICE_ELEMENT VALUE="1" CAPTION="That one" CHECKED="no" />
        </CHOICE>
        <CHOICE NAME="choiceelementwithotheroption" CAPTION="Which one?">
                <CHOICE_ELEMENT VALUE="0" CAPTION="This one" />
                <CHOICE_ELEMENT VALUE="1" CAPTION="That one" />
                <OTHER_FIELD VALUE="-1" CAPTION="Other, please specify" />
        </CHOICE>
        <MULTI_CHOICE NAME="choicemultielement" CAPTION="Which ones this
time? You can select more than one!">
               <MULTI_CHOICE_ELEMENT VALUE="0" CAPTION="This one" CHECKED="yes" 
/>
               <MULTI_CHOICE_ELEMENT VALUE="1" CAPTION="That one" CHECKED="yes" 
/>
               <MULTI_CHOICE_ELEMENT VALUE="2" CAPTION="Another one" 
CHECKED="yes" />
              <MULTI_CHOICE_ELEMENT VALUE="3" CAPTION="Yet another one as
well" CHECKED="no" />
              <OTHER_FIELD VALUE="" CAPTION="Other, please specify" 
CHECKED="yes" />
              <OTHER_FIELD VALUE="" CAPTION="Other, please specify" 
CHECKED="no" />
        </MULTI_CHOICE>
        <BOOLEAN NAME="boolelement2" CAPTION="Will it also?" CHECKED="yes" />
        <TEXT NAME="textelement" CAPTION="Enter some text:" />
        <TEXT NAME="numberelement" CAPTION="Enter a number:" NUMERICAL="yes" />
</MEASURE>

I basically want to output each element (BOOLEAN, CHOICE,
MULTI_CHOICE, TEXT...) to HTML which will differ according to what
type of element each is. The order in which they appear in the
document  should be maintained. My examples are merely printing out
the element names just now until I get the basic processing worked
out.

Now, I have tried this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
      version="1.0">
      
       <xsl:output method="html"/> 
       <xsl:template match="/">
        <html>
            <body>
                 <xsl:apply-templates select="MEASURE"/>
            </body>
            </html>
            </xsl:template>

              <xsl:template match="BOOLEAN">
                  <xsl:value-of select="@NAME"/><br></br>
              </xsl:template>
              
              <xsl:template match="CHOICE">
                  <xsl:value-of select="@NAME"/><br></br>
              </xsl:template>
              
              <xsl:template match="MULTI_CHOICE">
                  <xsl:value-of select="@NAME"/><br></br>
              </xsl:template>
              
              <xsl:template match="TEXT">
                  <xsl:value-of select="@NAME"/><br></br>
              </xsl:template>

My first MEASURE template:
<xsl:template match="MEASURE">
    <xsl:for-each select=".">
         <xsl:choose>
                   <xsl:when test="BOOLEAN">
                            <xsl:apply-templates select="BOOLEAN"/>
                    </xsl:when>
                     <xsl:when test="CHOICE">
                            <xsl:apply-templates select="CHOICE"/>
                      </xsl:when>
                      <xsl:when test="MULTI_CHOICE">
                            <xsl:apply-templates select="MULTI_CHOICE"/>
                      </xsl:when>
                      <xsl:when test="TEXT">
                            <xsl:apply-templates select="TEXT"/>
                      </xsl:when>
           </xsl:choose>
   </xsl:for-each>
</xsl:template>

Produced this HTML. Now it would appear that not all nodes have been
processed. Only those matching BOOLEAN, one of which was the first,
have been processed?
<html>
<head>
<body>
h2>boolelement<br>boolelement2<br>
</body>
</html>

My next attempt:
<xsl:template match="MEASURE">
          <xsl:for-each select=".">
                  <xsl:if test="BOOLEAN">
                       <xsl:apply-templates select="BOOLEAN"/>
                  </xsl:if>   
                  <xsl:if test="CHOICE">
                       <xsl:apply-templates select="CHOICE"/>
                   </xsl:if>
                    <xsl:if test="MULTI_CHOICE">
                       <xsl:apply-templates select="MULTI_CHOICE"/>
                    </xsl:if>
                    <xsl:if test="TEXT">
                       <xsl:apply-templates select="TEXT"/>
                    </xsl:if>
      </xsl:for-each>
</xsl:template>

was more successful:
<html>
<body>
<h2 align="center">Test all the available measure
elements</h2>boolelement<br>boolelement2<br>choiceelement<br>choiceelementwithotheroption<br>choicemultielement<br>textelement<br>numberelement<br>
</body>
</html>

 However the order is wrong. I have looked at the info regarding
sorting but can't quite grasp what I need to do. Also, why does the
first attempt fail so miserably, there's obviously something I'm not
quite grasping about conditional processing?

I know I have a lot to learn!

thanks and kind regards

Laing

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



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