Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

xml, xslt file bug

This is an example from WROX Beginning XML book. The xml and xslt files are below. I am not able to display the following using these files. Perhaps there is an error in these code pieces.

Question: Could you please make these code pieces work to produce this image (in a browser)?

Please do not post links etc. All I want is for someone test the code submitted and show me how it is done after debugging it.

User generated imageappUsersWithXslt.xml
<?xml-stylesheet type="text/xsl" href="appUsers.xslt" ?>
<applicationUsers>
  <user firstName="Joe" lastName="Fawcett" />
  <user firstName="Danny" lastName="Ayers" />
  <user firstName="Catherine" lastName="Middleton" />
</applicationUsers>

Open in new window

appUsers.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
        <head>
          <title>Application Users</title>
        </head>
        <body>
          <table>
            <thead>
              <tr>
                <th>First Name</th>
                <th>Last Name</th>
              </tr>
            </thead>
            <tbody>
              <xsl:apply-templates select="applicationUsers/user" />
            </tbody>
          </table>
        </body>
      </html>
    </xsl:template>

Open in new window

SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

Do you mean like:

           <tbody>    -- line 16
              <xsl:apply-templates select="applicationUsers/user" />  -- line 17
              <xsl:template match="user">
                    <tr>
                            <td><xsl:value-of select="@firstName" /></td>
                            <td><xsl:value-of select="@lastName" /></td>
                    </tr>
             </xsl:template>

            </tbody>  -- line 18

Trying it now.
No put it after line 21 in your XSLT that you posted earlier.

(But before the end element of your stylesheet which you omitted in the XSLT that you posted)
re:> (But before the end element of your stylesheet which you omitted in the XSLT that you posted)

I have copied from it from the book's sample code download. Here is what I have so far (added after line 21) but I am not quite sure how I need to close it. I will post this error on their site after having spend over 4 hours on it.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
        <head>
          <title>Application Users</title>
        </head>
        <body>
          <table>
            <thead>
              <tr>
                <th>First Name</th>
                <th>Last Name</th>
              </tr>
            </thead>
            <tbody>
              <xsl:apply-templates select="applicationUsers/user" />
            </tbody>
          </table>
        </body>
      </html>
    </xsl:template>

    <xsl:template match="user">
      <tr>
        <td><xsl:value-of select="@firstName" /></td>
        <td><xsl:value-of select="@lastName" /></td>
      </tr>
    </xsl:template>
</closing tag here???>

Open in new window


Thanks for the help.
I downloaded the current version of that code from the PDF sample of the first chapter of version 5 of the book.  Apparently the book was written with Microsoft Word because all of the double quotes were 'smart quotes' which don't work in code.

Here are the two files and they work in Firefox.
appUsers.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <html>
        <head>
          <title>Application Users</title>
        </head>
        <body>
          <table>
            <thead>
              <tr>
                <th>First Name</th>  
                <th>Last Name</th>
              </tr>
            </thead>
            <tbody>
              <xsl:apply-templates select="applicationUsers/user" />
            </tbody>
          </table>
        </body>
      </html>
    </xsl:template>
  <xsl:template match="user">
    <tr>
      <td>
        <xsl:value-of select="@firstName"/>
      </td>
      <td>
        <xsl:value-of select="@lastName"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Open in new window

appUsersWithXslt.xml
<?xml-stylesheet type="text/xsl" href="appUsers.xslt" ?>
<applicationUsers>
  <user firstName="Joe" lastName="Fawcett" />
  <user firstName="Danny" lastName="Ayers" />
  <user firstName="Catherine" lastName="Middleton" />
</applicationUsers>

Open in new window

Nothing special... You've opened the <xsl:stylesheet> tag at the very top, so you must close it...

</xsl:stylesheet>

Open in new window

No luck yet.

Would you please test it to see if it works for you? If so, then paste the working code here.
re:> I downloaded the current version of that code from the PDF sample of the fir...

You could also download sample codes I suppose. Maybe because I have purchased the book it is available to me but not without purchase. I am not sure.
Chapter 1 with the wrong quotes is here: http://media.wiley.com/product_data/excerpt/37/11181621/1118162137-143.pdf   All I changed were their 'smart quotes' to regular quotes.  The code I posted above is correct.
By the way, the version above works in all browsers I have, even IE8.
It is not working for me. Does it work when you try?
And yes I did test it myself too and it works fine.

If it is still not working then it is an issue on your end. You're not using Chrome are you? It won't work.
Yes, it works for me.  As I said, in all browsers, Firefox, Chrome, Opera, and even IE8.  I just checked in IE11 on both IIS7 and Apache 2.4.  Works everywhere.
I tried with explorer, still no luck. There is a note:

"WARNING If you are using Internet Explorer for this or other activities, you’ll probably have to go to ToolsInternet Options and choose the Advanced tab. Under the Security section, check the box in front of Allow Active Content to Run in Files on My Computer. This effectively allows script to work on local files."

But in the latest version of explorer they are hidden somewhere I can't find my way to them. trying. See the image.
Settings.png
Dave, what version of Chrome have you got it working with? And are you just opening as a local file or are you serving them from a server?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a link to the files on one of my hosting accounts:  http://www.yamiam.org/appUsersWithXslt.xml
Internet Options is still there but you have to enter it in the Control Panel search bar to get it to show up.  Or in IE11, you can click on the gear symbol and it is the next to the last item in the dropdown list.
Fire fox worked. I may need to upgrade my Chrome.

Thanks,

Mike
You're welcome, glad to help.
Mike, You're welcome!

Dave, I tried with both Chrome v44.0.2403.157 and updated to the latest, v45.0.2454.85 opening the file directly and both give an error in the console "Unsafe attempt to load URL file:///C:/Temp/ee/appUsers.xslt from frame with URL file:///C:/Temp/ee/appUsersWithXslt.xml. Domains, protocols and ports must match." which when I search on that, it seems it's a common well known issue with Chrome. Just wondering how you might be getting around that (other than serving it from a server) as it is something I need to do reasonably often and I hate having Firefox installed just for that! :)
Actually, I hadn't tried loading it in Chrome from a file.  I just tried it and I get nothing, just a blank screen with no error message.  Chrome is very restrictive about loading anything with a 'file://' protocol.

On the other hand, as a web programmer, none of my work gets loaded as a 'file://' so I rarely test it that way.  While HTML usually works that way, IE doesn't like active content like JavaScript and PHP and ASP simply won't run.  The only time I use 'file://' is when I need to write something that runs locally without a server like help pages.

And because my customers have all the browsers, so do I.  Chrome seems to be the fastest, Firefox is the most useful for development (best and most addons), Opera is the odd one, and IE is often the slowest but is required for testing because so many people use it.  There are more but those are the most mainstream.  Oh, and Safari though I don't have a really recent version.  Can't afford a new Mac.
Ah, ok, no worries. I will continue with Firefox then. Yeah, it's only for quick and dirty XSL tests that I can't be bothered firing up my whole Java app for, and yes I'm too tight to shell out for a proper XML IDE! ;)
For what it's worth, IE and Firefox will load it from a file, Chrome and Opera will not.  Didn't try Safari.