Help with Basic XML example

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • delcov
    New Member
    • Sep 2007
    • 5

    Help with Basic XML example

    I am trying to learn XML and interacting w/ C# and ASP.NET forms. I am not getting the output I expect, and wanted to pose that question of "why" to the group here.

    My XSLT:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="html"/>
    <xsl:template match="/">
    <html>
    <head>
    <style type="text/css">
    .redText {font-family:arial;co lor:#ff0000;}
    .borders {border-left:1px solid #0000ff;
    border-right:1px solid #00ff00;
    border-top:1px solid #00ff00;
    border-bottom:1px solid #0000ff;}
    </style>
    </head>
    <body bgcolor="#00000 0">
    <table class="borders" border="0" width="800" cellpadding="4"
    cellspacing="0" bgcolor="#efefe f">
    <xsl:apply-templates select="menuBar "/>
    <tr height="100%">
    <td>News Goes Here<br/>
    <xsl:apply-templates select="News"/>
    </td>
    <td class="redText" ><xsl:apply-templates select="MainSec tion"/></td>
    </tr>
    </table>
    </body>
    </html>
    </xsl:template>
    <xsl:template match="News">
    <table class="borders" border="0" width="640" cellpadding="4"
    cellspacing="0" bgcolor="#ff88c c">
    <tr><td></td></tr>
    </table>
    </xsl:template>
    <xsl:template match="MainSect ion">
    <span>Hello World!</span>
    <p/>
    </xsl:template>
    <xsl:template match="menuBar" >
    <tr>
    <td>
    <table class="borders" border="0" width="640" cellpadding="4"
    cellspacing="0" bgcolor="#ff88c c">
    <tr>
    <td>register</td>
    <td>menu item 1</td>
    </tr>
    </table>
    </td>
    </tr>
    </xsl:template>
    </xsl:stylesheet>




    My C# PageLoad Method:

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // Put user code to initialize the page here
    datasets.UserIn foDS ds = new datasets.UserIn foDS();
    XmlDataDocument dd = new XmlDataDocument (ds);

    string xslPath = Server.MapPath( "CommonTemplate .xslt");
    StringWriter sw = new StringWriter();
    XslTransform xsl = new XslTransform();
    xsl.Load(xslPat h);


    //Instantiate the XPathDocument Class
    //XPathDocument doc = new XPathDocument(x mlPath);


    //Instantiate the XslTransform Class
    xsl.Transform(d d,null,sw,null) ;

    Response.Write( sw.ToString());
    }




    But I'm getting the following as output:

    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=utf-16">
    <style type="text/css">
    .redText {font-family:arial;co lor:#ff0000;}
    .borders {border-left:1px solid #0000ff;
    border-right:1px solid #00ff00;
    border-top:1px solid #00ff00;
    border-bottom:1px solid #0000ff;}
    </style>
    </head>
    <body bgcolor="#00000 0">
    <table class="borders" border="0" width="800" cellpadding="4" cellspacing="0" bgcolor="#efefe f">
    <tr height="100%">
    <td>News Goes Here<br></td>
    <td class="redText" >
    </td>
    </tr>
    </table>
    </body>
    </html>

    I am expecting to at least see menubar words, the table under the News area, and the words Hello World in the MainSection. None of those show up. Any ideas? The dataset is just a blank dataset.

    Thanks!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    My guess is:
    <xsl:apply-templates select="News"/>
    is not properly matching up.

    I'm guessing the News node is at the wrong level.
    You may want to try xpath "//News" or "*/News"

    if it's a namespace problem (xmlns="..."), try "//*[local-name()='News']"

    Comment

    • delcov
      New Member
      • Sep 2007
      • 5

      #3
      Since I am using a blank dataset, will that affect the fact that the menuBar, News and MainSection templates are not being applied? But my assumption was that since I'm calling "apply-template", it would attempt to apply the template regardless of the data pushed to the page. By that I mean, if I apply the News template, and the news template has an encompassing table around dynamically generated "NewsItem" Rows, if you have no NewsItem rows, you would at least see the encompassing table.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        The way you've called the template is node driven, matching only News nodes.

        You'd want to use call-template with named templates instead. eg:
        <xsl:call-template name="News"/>

        And then have your template:
        <xsl:template name="News">


        Comment

        • delcov
          New Member
          • Sep 2007
          • 5

          #5
          Thanks for your reply. I think I'm closer to my answer. Now, I get the error message 'xsl:call-templates' cannot be a child of 'td' element when I run my web app. I'm assuming that I'm using call-templates incorrectly, that it's not meant to just pull a template into the spot where it's called. That was my intent in the xslt file. Basically, i wanted to see a webpage where the menu bar goes across the top of the page, and underneath that are 2 sections, a news section and the Main section, where each section will eventually be dynamic. Right now, the dataset hasn't been built to pass in menuBar objects or News objects.

          Comment

          • delcov
            New Member
            • Sep 2007
            • 5

            #6
            Ok. I've done some reading, thought about it, and modified my code in this way. I'm still not getting the results I'm expecting. I'm providing the info below:


            XSLT:

            <?xml version="1.0" encoding="UTF-8" ?>
            <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" version="1.0">
            <xsl:output indent="yes" method="html"/>
            <xsl:template match="/">
            <html>
            <head>
            <style type="text/css">
            .redText {font-family:arial;co lor:#ff0000;}
            .borders {border-left:1px solid #0000ff;
            border-right:1px solid #00ff00;
            border-top:1px solid #00ff00;
            border-bottom:1px solid #0000ff;}
            </style>
            </head>
            <body bgcolor="#00ff0 0">
            <xsl:apply-templates/>
            </body>
            </html>
            </xsl:template>
            <xsl:template name="Page">
            <table class="borders" border="0" width="800" cellpadding="4" cellspacing="0" bgcolor="#efefe f">
            <tr>
            <xsl:apply-templates select="menuBar "/>
            </tr>
            <tr height="100%">
            <td>News Goes Here<br/>
            <xsl:apply-templates select="NewsIte m"/>
            </td>
            <td class="redText" ><xsl:apply-templates select="MainFor m"/></td>
            </tr>
            </table>
            </xsl:template>
            <xsl:template name="NewsItem" >
            <table class="borders" border="0" width="640" cellpadding="4"
            cellspacing="0" bgcolor="#ff88c c">
            <tr><td></td></tr>
            </table>
            </xsl:template>
            <xsl:template name="MainForm" >
            <span>Hello World!</span>
            <p/>
            </xsl:template>
            <xsl:template name="menuBar">
            <td>register</td>
            <td>menu item 1</td>
            </xsl:template>
            </xsl:stylesheet>





            The XML (generated from an ADO.NET dataset):

            <?xml version="1.0" standalone="yes "?>
            <Page xmlns="http://tempuri.org/Page.xsd">
            <menuBar>
            <name>registe r</name>
            <URL>http://register.com</URL>
            </menuBar>
            <MainForm>
            <title>Home.asp x</title>
            </MainForm>
            <NewsItem>
            <description>De scription of news item</description>
            <story>The story of the item goes here.</story>
            </NewsItem>
            </Page>



            The results i get are:

            <html>
            <head>
            <META http-equiv="Content-Type" content="text/html; charset=utf-16">
            <style type="text/css">
            .redText {font-family:arial;co lor:#ff0000;}
            .borders {border-left:1px solid #0000ff;
            border-right:1px solid #00ff00;
            border-top:1px solid #00ff00;
            border-bottom:1px solid #0000ff;}
            </style>
            </head>
            <body bgcolor="#00ff0 0">registerhttp ://register.comHom e.aspxDescripti on of news itemThe story of the item goes here.</body>
            </html>


            It looks like it's just spitting the data from my dataset out at the location in the XSLT where I call <xsl:apply-templates/>. Can anyone help me to figure this out??? Thanks!

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              call-template not
              call-templates

              Change:
              <xsl:apply-templates/>
              to
              <xsl:call-template name="Page"/>

              <xsl:apply-templates select="menuBar "/>
              to
              <xsl:call-templates name="menuBar"/>


              I'm assuming NewsItem is your repeating node in your dataset.

              Comment

              • delcov
                New Member
                • Sep 2007
                • 5

                #8
                Your assumption is correct. And the code now works. Thanks for your help and patience. I'm slowly learning how to manipulate XML, and need to read the specs I guess. Thanks again.

                Comment

                • Sprotty
                  New Member
                  • Oct 2007
                  • 5

                  #9
                  XML Studio (which is free) contains an XPath expression builder, which also provides sample code showing how to use the expression in XSLT, java, C# etc.
                  This is particularly handy with complex expressions or namespaces...

                  http://www.liquid-technologies.co m

                  Originally posted by jkmyoung
                  My guess is:
                  <xsl:apply-templates select="News"/>
                  is not properly matching up.

                  I'm guessing the News node is at the wrong level.
                  You may want to try xpath "//News" or "*/News"

                  if it's a namespace problem (xmlns="..."), try "//*[local-name()='News']"

                  Comment

                  Working...