Solved

building html into my xslt stylesheet

Posted on 2010-08-26
19
212 Views
Last Modified: 2012-05-10
I have the following simple stylesheet that i transform using vb.net to integrate and display some xml:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
   
    <table border="0">      
      <xsl:for-each select="Orders/Order">
        <tr>
          <td>
            <xsl:value-of select="CustomerName"/>
            <br/>
           
            <xsl:if test="DeliveryCompany != ''">
              <xsl:value-of select="DeliveryCompany"/>
              <br/>
            </xsl:if>

            <xsl:value-of select="DeliveryStreet1"/><br/>

            <xsl:if test="DeliveryStreet2 != ''">
              <xsl:value-of select="DeliveryStreet2"/>
              <br/>
            </xsl:if>

            <xsl:value-of select="DeliveryCity"/><br/>
            <xsl:value-of select="DeliveryPostalCode"/>
           
          </td>
        </tr>
      </xsl:for-each>

    </table>

  </xsl:template>

</xsl:stylesheet>


This works fine.

However if I try and add a <h1> tag etc I start getting errors.

What do I need to add to allow me to be able to input more html?
0
Comment
Question by:scm0sml
  • 9
  • 7
  • 3
19 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33531455
check the following example for html embedded in xslt:(http://www.ibm.com/developerworks/xml/library/x-tiphtml.html)<?xml version="1.0"?>     version="1.0">Select Choices<script type="text/javascript" version="1.3">function checkDate() {   var today = new Date();   var deadlineDate = new Date('3/3/2002');   if (today > deadlineDate) {        alert('The deadline has passed.');        return false;   } else {        return true;   }   }</script><form action="processform.jsp" method="post" onsubmit="return checkDate()">We will send you information on the subjects in which you'reinterested. Please check all that apply.
      <input type="submit" value="Submit Choices"/></form>            checkbox                                    checked                        
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33531489
Should work as long as it's well formed. What is the error your getting?
0
 

Author Comment

by:scm0sml
ID: 33531682
the error im getting is:
Token StartElement in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33531723
can you paste the whole thing here?
0
 

Author Comment

by:scm0sml
ID: 33531733
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>

      <head>
        <title></title>
      </head>
    </html>
    <body>
    <h1>next</h1>
    <table border="0">      
      <xsl:for-each select="Orders/Order">
        <tr>
          <td>
            <xsl:value-of select="CustomerName"/>
            <br/>
           
            <xsl:if test="DeliveryCompany != ''">
              <xsl:value-of select="DeliveryCompany"/>
              <br/>
            </xsl:if>

            <xsl:value-of select="DeliveryStreet1"/><br/>

            <xsl:if test="DeliveryStreet2 != ''">
              <xsl:value-of select="DeliveryStreet2"/>
              <br/>
            </xsl:if>

            <xsl:value-of select="DeliveryCity"/><br/>
            <xsl:value-of select="DeliveryPostalCode"/>
           
          </td>
        </tr>
      </xsl:for-each>

    </table>
    </body>
  </xsl:template>

</xsl:stylesheet>


0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33531735
Do you get the error in your xslt designer or in the web page when your doing the transformation? Can you post the xslt that generate the error.
0
 

Author Comment

by:scm0sml
ID: 33531750
when i do the transform! just posted the xslt :)
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33531772
try this:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>

      <head>
        <title></title>
      </head>
 
    <body>
    <h1>next</h1>
    <table border="0">      
      <xsl:for-each select="Orders/Order">
        <tr>
          <td>
            <xsl:value-of select="CustomerName"/>
            <br/>
           
            <xsl:if test="DeliveryCompany != ''">
              <xsl:value-of select="DeliveryCompany"/>
              <br/>
            </xsl:if>

            <xsl:value-of select="DeliveryStreet1"/><br/>

            <xsl:if test="DeliveryStreet2 != ''">
              <xsl:value-of select="DeliveryStreet2"/>
              <br/>
            </xsl:if>

            <xsl:value-of select="DeliveryCity"/><br/>
            <xsl:value-of select="DeliveryPostalCode"/>
           
          </td>
        </tr>
      </xsl:for-each>

    </table>
    </body>
   </html>
  </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33531836
try adding  below
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:scm0sml
ID: 33531973
well i've now got:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
 
  <xsl:template match="/">
   
    <html>

      <head>
        <title></title>
      </head>

      <body>
        <h1>next</h1>
        <table border="0">
          <xsl:for-each select="Orders/Order">
            <tr>
              <td>
                <xsl:value-of select="CustomerName"/>
                <br/>

                <xsl:if test="DeliveryCompany != ''">
                  <xsl:value-of select="DeliveryCompany"/>
                  <br/>
                </xsl:if>

                <xsl:value-of select="DeliveryStreet1"/>
                <br/>

                <xsl:if test="DeliveryStreet2 != ''">
                  <xsl:value-of select="DeliveryStreet2"/>
                  <br/>
                </xsl:if>

                <xsl:value-of select="DeliveryCity"/>
                <br/>
                <xsl:value-of select="DeliveryPostalCode"/>

              </td>
            </tr>
          </xsl:for-each>

        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>


its not erroring anymore but itsn not displaying anything either? :(
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33532402
By anything do you mean the "next" is not displaying or nothing at all is displaying? Can you post your transformation code.
0
 

Author Comment

by:scm0sml
ID: 33532545
before i made the cahnges in this post i was able to display the html table etc but was getting errors if i added a <h1> etc.

Now I can add the h1 without error but nothing at all is being displayed so i think the issue is def in the xslt.

I have reposted this below along with my transformation code.

Thanks

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
 
  <xsl:template match="/">
   
    <html>

      <head>
        <title></title>
      </head>

      <body>
        <h1>next</h1>
        <table border="0">
          <xsl:for-each select="Orders/Order">
            <tr>
              <td>
                <xsl:value-of select="CustomerName"/>
                <br/>

                <xsl:if test="DeliveryCompany != ''">
                  <xsl:value-of select="DeliveryCompany"/>
                  <br/>
                </xsl:if>

                <xsl:value-of select="DeliveryStreet1"/>
                <br/>

                <xsl:if test="DeliveryStreet2 != ''">
                  <xsl:value-of select="DeliveryStreet2"/>
                  <br/>
                </xsl:if>

                <xsl:value-of select="DeliveryCity"/>
                <br/>
                <xsl:value-of select="DeliveryPostalCode"/>

              </td>
            </tr>
          </xsl:for-each>

        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>


here is the transform code from vb.net:
oXml.LoadXml(xml)

        oWriter = XmlWriter.Create(htmlFileName)
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

        xsltrans.Load(Application.StartupPath & "\" & xsltFilename & ".xslt", Nothing, resolver)
        xsltrans.Transform(oXml, oWriter)
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33532884
try it without the resolver, I don't believe you need this for what your doing.

xsltrans.Load(Application.StartupPath & "\" & xsltFilename & ".xslt")

what version of .net are you using?
0
 

Author Comment

by:scm0sml
ID: 33532996
2.0
0
 

Author Comment

by:scm0sml
ID: 33533016
removed the resolver but still no joy :(
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 33533168
Assuming your XML is good (you can open the XML file in IE and it looks good)

I would do this, create a simple aspx page in your sites root, put your XML and XSL files in the root as well

   

If this works then the issue is probably with the transformation code, if not it's probably the XML and XSL itself.
0
 

Author Comment

by:scm0sml
ID: 33540001
this is a windows based application.

bit of a breakthrough though. The html file I am creating doesnt show the output when opening or printing it, but if i view source it is all there!!

I've included the first few lines of the html as presumably this is where the problem is?

<?xml version="1.0" encoding="utf-8"?><html xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:z="#RowsetSchema"><head><title /></head><body>
        test
    <table><tr><td><img src="" alt="" /></td><td>
                Some address etc<br />
0
 

Author Comment

by:scm0sml
ID: 33540247
right i have found what the problem was.

i had my title tage <title></title>

But because it was emty it was bring transformed as <title/> and this meant that the html was displaying.

I have now put some dummy text in but is there a fix for this?
0
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 500 total points
ID: 33542873
remove it, you don't need it on a windows app, it won't show anyway.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now