Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

building html into my xslt stylesheet

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
scm0sml
Asked:
scm0sml
  • 9
  • 7
  • 3
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
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
 
Bob HoffmanDeveloperCommented:
Should work as long as it's well formed. What is the error your getting?
0
 
scm0smlAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Meir RivkinFull stack Software EngineerCommented:
can you paste the whole thing here?
0
 
scm0smlAuthor Commented:
<?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
 
Bob HoffmanDeveloperCommented:
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
 
scm0smlAuthor Commented:
when i do the transform! just posted the xslt :)
0
 
Meir RivkinFull stack Software EngineerCommented:
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
 
Bob HoffmanDeveloperCommented:
try adding  below
0
 
scm0smlAuthor Commented:
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
 
Bob HoffmanDeveloperCommented:
By anything do you mean the "next" is not displaying or nothing at all is displaying? Can you post your transformation code.
0
 
scm0smlAuthor Commented:
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
 
Bob HoffmanDeveloperCommented:
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
 
scm0smlAuthor Commented:
2.0
0
 
scm0smlAuthor Commented:
removed the resolver but still no joy :(
0
 
Bob HoffmanDeveloperCommented:
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
 
scm0smlAuthor Commented:
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
 
scm0smlAuthor Commented:
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
 
Bob HoffmanDeveloperCommented:
remove it, you don't need it on a windows app, it won't show anyway.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 9
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now