Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there something wrong with my XML? It wont use any of the stylesheet's I try to apply to it.
This is what my xml looks like.
XML
<?xml version="1.0"?>
<?xml-stylsheet type="text/xsl"
                href="Documentation.xsl"?>
<Root>
  <Item>
    <OrderId>Item Code</OrderId>
    <ItemDescription>Item Description</ItemDescription>
    <CurrentCount>Current Count</CurrentCount>
    <OnOrder>On Order</OnOrder>
  </Item>
  <Item>
    <OrderId>A0001</OrderId>
    <ItemDescription>asdjfasdaf</ItemDescription>
    <CurrentCount>5</CurrentCount>
    <OnOrder>No</OnOrder>
  </Item>


Then my stylesheets are all similar to this:
XML
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version ="2.0">
  
  <!--this is the first stylesheet output option-->
  <xsl:template match="/root">
    <html>
      <head>
        <title>Inventory List</title>
      </head>
      <body>
        <table border="1">
          <tr>
            <th>OrderId</th>
            <th>ItemDescription</th>
            <th>CurrentCount</th>
            <th>OnOrder</th>
          </tr>
          <xsl:for-each select="Inventory/Item">
            <tr>
              <td>
                <xsl:value-of select="OrderId"/>
              </td>
              <td>
                <xsl:value-of select="ItemDescription"/>
              </td>
              <td>
                <xsl:value-of select="CurrentCount"/>
              </td>
              <td>
                <xsl:value-of select="OnOrder"/>
              </td>
            </tr>


          </xsl:for-each>
        </table>
      </body>
    </html>

  </xsl:template>
</xsl:stylesheet>


What I have tried:

I have tried changing the href and the location the files are stored. I cant seem to find a reason why it wont work.
Any help would be greatly appreciated
Posted
Updated 27-May-16 18:34pm

1 solution

There are a few errors in both your XML file and the style sheet.

1. In the XML
XML
<?xml-stylsheet type="text/xsl" href="Documentation.xsl"?>

should be
XML
<?xml-stylesheet type="text/xsl" href="Documentation.xsl"?>

stylesheet misspelled

2. In the XSLT
XML is case sensitive, so the statement
XML
<xsl:template match="/root" xmlns:xsl="#unknown"></xsl:template>

should be
XML
<xsl:template match="/Root" xmlns:xsl="#unknown"></xsl:template>


2.1 Also consider to add the statement
XML
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes" />

to your XSLT file.
See XSLT <xsl:output> Element[^]

2.2 XSLT is recursive by nature
XML
<table border="1">
  <tr>
    <th>OrderId</th>
    <th>ItemDescription</th>
    <th>CurrentCount</th>
    <th>OnOrder</th>
  </tr>
  <xsl:for-each select="Inventory/Item">
    <tr>
      <td>
        <xsl:value-of select="OrderId"/>
      </td>
      <td>
        <xsl:value-of select="ItemDescription"/>
      </td>
      <td>
        <xsl:value-of select="CurrentCount"/>
      </td>
      <td>
        <xsl:value-of select="OnOrder"/>
      </td>
    </tr>


  </xsl:for-each>
</table>

should be changed to
XML
<table border="1">
  <tr>
    <th>OrderId</th>
    <th>ItemDescription</th>
    <th>CurrentCount</th>
    <th>OnOrder</th>
  </tr>
  <xsl:apply-templates select="Item" />
</table>

and add
XML
<xsl:template match="Item">
  <tr>
    <td>
      <xsl:value-of select="OrderId"/>
    </td>
    <td>
      <xsl:value-of select="ItemDescription"/>
    </td>
    <td>
      <xsl:value-of select="CurrentCount"/>
    </td>
    <td>
      <xsl:value-of select="OnOrder"/>
    </td>
  </tr>
</xsl:template>



One last thing. Are you sure about the XSLT version?
Most browsers only support version 1.0
XML
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">version ="2.0"></xsl:stylesheet>
 
Share this answer
 
v4
Comments
Member 12546783 28-May-16 0:47am    
thanks mate ill give it a go. This is my first time looking at xml/xslt, so I appreciate the help!
Member 12546783 28-May-16 0:48am    
working perfectly now! your a legend!!!
George Jonsson 28-May-16 0:51am    
You are welcome.
XSLT is a different beast, so it takes time to get the hang of it.
Most of the time you should NOT use for-each.
Karthik_Mahalingam 28-May-16 0:49am    
5, clean
George Jonsson 28-May-16 0:52am    
Thanks Karthik

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900