Shouldn't this be simple?

Last post 07-05-2005 1:03 PM by KraGiE. 2 replies.

Sort Posts:

  • Shouldn't this be simple?

    07-04-2005, 5:42 PM
    • Member
      24 point Member
    • mdleichty
    • Member since 07-04-2005, 9:36 PM
    • Posts 6

    I'm just starting to delve into both ASP.NET and XML.  My XML file has this:

    <?xml version="1.0" encoding="iso-8859-1"?>
     <meals>

      <meal id="Breakfast">
       <foods>
        <food>Corn Flakes Cereal</food>
        <food>Frozen Sweet Rasberries</food>
        <food>Soy Milk</food>
       </foods>
       <calories>215</calories>
      </meal>

      <meal id="Lunch">
       <foods>
        <food>Hard Roll</food>
        <food>Orange</food>
        <food>Butternut Squash with Ginger</food>
       </foods>
       <calories>342</calories>
      </meal>

      <meal id="Dinner">
       <foods>
        <food>Cooked Couscous</food>
        <food>Raw Broccoli</food>
        <food>Curried Chicken with Almonds</food>
       </foods>
       <calories>538</calories>
      </meal>

      <meal id="Snacks">
       <foods>
        <food>Orange</food>
        <food>Sunflower seeds</food>
       </foods>
       <calories>233</calories>
      </meal>

     </meals>

    ... And I want my ASP.NET page to output this

    Breakfast (215 calories)
        Corn Flakes Cereal, Frozen Sweet Rasberries, Soy Milk

    Lunch (342 calories)
       Hard Roll, Orange, Butternut Squash with Ginger

    etc.

    Obviously, there will not always be three items (or any items), or there may not be snacks on a particular day.  I'm not sure how to use the datalist / datagrid / repeater to achieve this simply. 

  • Re: Shouldn't this be simple?

    07-05-2005, 7:07 AM
    • Participant
      860 point Participant
    • JasonFollas
    • Member since 06-02-2005, 6:13 PM
    • 41.501000, -83.718000
    • Posts 170

    Unfortunately, ASP.NET is not particularly my forte, so I can't just simply tell you how to do it using controls.  However, I have worked with XSLT quite a bit in the past, and that sounds perfect for this scenario.

    XSLT is a transformation technology.  It's purpose is to transform your original tree into another tree, but the output is text, and thus, does not necessarily need to be well-formed XML.  So, my code's goal is to transform your XML into an HTML snippet which can then be inserted into your web page, either via the Response stream, or some other method.

    First, the .NET code:


       System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
       xmlDoc.Load(@"c:\meals.xml");

       System.Xml.Xsl.XslTransform xslt = new System.Xml.Xsl.XslTransform();
       xslt.Load(@"c:\meals.xsl");
       
       System.Xml.XmlReader reader = xslt.Transform(xmlDoc, null, new System.Xml.XmlUrlResolver());

       reader.MoveToContent();

       string results = reader.ReadInnerXml();



    Next, is the contents of the "meals.xsl" file.  I use templates here, which allows for the maximum flexibility in customizing how each individual node in your XML tree is rendered.  Note that "&nbsp;" is not an XML entity, so you need to use the character code equivilent of the non-breaking space, "&#160;".

    <xsl:stylesheet   version="1.0"
                      xmlns:xsl="
    http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">

       <xsl:apply-templates select="meals" />
       
    </xsl:template>


    <xsl:template match="meals">
    <meals>
       <xsl:apply-templates select="meal[@id='Breakfast']" />
       <xsl:apply-templates select="meal[@id='Lunch']" />
       <xsl:apply-templates select="meal[@id='Dinner']" />
       <xsl:apply-templates select="meal[@id='Snacks']" />
    </meals>
    </xsl:template>


    <xsl:template match="meal">
       <xsl:value-of select="@id" /> (<xsl:value-of select="calories" /> Calories) <br/>
       &#160;&#160;&#160; <xsl:apply-templates select="foods" /> <br />
       <br />
      
    </xsl:template>


    <xsl:template match="foods">
       <xsl:apply-templates select="food" />
    </xsl:template>

    <xsl:template match="food">
       <xsl:value-of select="." /><xsl:if test="position() != last()">, </xsl:if>
    </xsl:template>

    </xsl:stylesheet>



    The output of the transformation is the following (not exactly intended for human consumption, but not too bad):

    <meals>
    Breakfast (215 Calories) <br />
           Corn Flakes Cereal, Frozen Sweet Rasberries, Soy Milk<br /><br />Lunch (342 Calories) <br />
           Hard Roll, Orange, Butternut Squash with Ginger<br /><br />Dinner (538 Calories) <br />
           Cooked Couscous, Raw Broccoli, Curried Chicken with Almonds<br /><br />Snacks (233 Calories) <br />
           Orange, Sunflower seeds<br /><br />
    </meals>



    From this, we grab the contents of the <meals> node, which is then inserted into your rendering web page. 

    The resulting rendered HTML looks like:
    ___________________________________________________________________________
    Breakfast (215 Calories)
        Corn Flakes Cereal, Frozen Sweet Rasberries, Soy Milk

    Lunch (342 Calories)
        Hard Roll, Orange, Butternut Squash with Ginger

    Dinner (538 Calories)
        Cooked Couscous, Raw Broccoli, Curried Chicken with Almonds

    Snacks (233 Calories)
        Orange, Sunflower seeds
    ___________________________________________________________________________

  • Re: Shouldn't this be simple?

    07-05-2005, 1:03 PM
    • Star
      14,455 point Star
    • KraGiE
    • Member since 08-16-2002, 11:11 AM
    • Orange County
    • Posts 2,890
    If the output is complex or will frequently change, I'd go with XSL, but to simply output the contents, I'd probably just loop the nodes, and output directly through code.

    No reason to have double IO reads.  :)

    Kay Lee
    MySpace.com - http://www.myspace.com/kragie
    Infrastructure Group
    MySpace.com

    - Code to live, but Live to code.
Page 1 of 1 (3 items)