Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read a XML file by modified XSL

Posted on 2013-12-25
8
Medium Priority
?
343 Views
Last Modified: 2013-12-26
<?xml version="1.0" encoding="iso-8859-1" ?><?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<catalog>
      <cd id=1>
            <title>Empire Burlesque</title>
            <artist>Bob Dylan</artist>
            <country>USA</country>
            <company>Columbia</company>
            <price>10.90</price>
            <year>1985</year>
      </cd>
      <cd id=2>
            <title>Hide your heart</title>
            <artist>Bonnie Tyler</artist>
            <country>UK</country>
            <company>CBS Records</company>
            <price>9.90</price>
            <year>1988</year>
      </cd>
      <cd id=3>
            <title>Greatest Hits</title>
            <artist>Dolly Parton</artist>
            <country>USA</country>
            <company>RCA</company>
            <price>9.90</price>
            <year>1982</year>
      </cd>
</catalog>

How to read the above xml file by modified below stylesheet?

[<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>ID</th>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd/@">
      <tr>
        <td><xsl:value-of select="../@attr1"/></td>  
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
0
Comment
Question by:andrew_man
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:zc2
ID: 39739513
If the xsl is in a file called "sample.xsl" in the same folder as the xml, then you can open the xml in a browser and the xsl will be used. Because the xml  references to it by name.

To execute the xslt transformation from the command line you could try an utility called msxsl (search for it on the Microsoft downloads site).

Describe your task and the environment here to help you more specific.
0
 
LVL 4

Author Comment

by:andrew_man
ID: 39739519
All are ready and located at same folder.  But, I don't know how to call the attribute.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39739792
You are not very precise of what exactly your problem is.
I see some possible issues

- The XML is not wellformed. XML requires attribute value boundaries to have balanced " or '
<cd id=1>
should be
<cd id="1">
on every CD
your above XML simply will not load

- your XSLT seems to have a [ in front,
if that [ is in your sample.xsl, the XSLT will not load

- you are not indicating which attribute you need in the for each
                   <xsl:for-each select="catalog/cd/@">
the short form @ for attribute:: needs a qualifier
@* or @id
                   <xsl:for-each select="catalog/cd/@*">
but I don't think you want to visit the attribute nodes, but rather the cd that have an id attribute, so you will need this
                   <xsl:for-each select="catalog/cd[@id]">

- <td><xsl:value-of select="../@attr1"/></td>
in your original question will not do anything in the context of an attribute because you don't have attributes named attr1
<td><xsl:value-of select="title"/></td>
will do nothing since attributes don't have children
Here is the entire snippet corrected
                    <xsl:for-each select="catalog/cd[@*]">
                        <tr>
                            <td><xsl:value-of select="@id"/></td>  
                            <td><xsl:value-of select="title"/></td>
                            <td><xsl:value-of select="artist"/></td>
                        </tr>
                    </xsl:for-each>

Open in new window


Having added all these corrections... you need to be aware that generally browsers can be cautious doing this and some browsers might not read both XML and XSLT from a local disk. If I want to test things like this cross browsers, I always put them behind a server. No security issues then
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Author Comment

by:andrew_man
ID: 39739833
It is still not works!  Please help!

<?xml version="1.0" encoding="iso-8859-1" ?><?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<catalog>
	<cd id="1">
		<title>Empire Burlesque</title>
		<artist>Bob Dylan</artist>
		<country>USA</country>
		<company>Columbia</company>
		<price>10.90</price>
		<year>1985</year>
	</cd>
	<cd id="2">
		<title>Hide your heart</title>
		<artist>Bonnie Tyler</artist>
		<country>UK</country>
		<company>CBS Records</company>
		<price>9.90</price>
		<year>1988</year>
	</cd>
	<cd id="3">
		<title>Greatest Hits</title>
		<artist>Dolly Parton</artist>
		<country>USA</country>
		<company>RCA</company>
		<price>9.90</price>
		<year>1982</year>
	</cd>
</catalog>

Open in new window


[<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>ID</th>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd[@*]">
      <tr>
        <td><xsl:value-of select="@id"/></td>  
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39739842
"It is still not works" is not very helpfull.
What does not work?
Note that you still have the "[" in front of your XSLT, remove it and try again

After I removed the [ from your XSLT and test it, it works perfectly
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39739848
here is what Firefox shows meyour xml and xsl in firefox
0
 
LVL 4

Author Comment

by:andrew_man
ID: 39739854
Would you mind to post this version here?
Thanks!
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 39739857
what versions?
I have made the above screenshot with the XML and XSLT that I took from your posts, except that I removed the [ from the XSLT lead

As I said, I put stuff on a server to test
http://c-moria.com/sample.xml
you can download the xsl from there too
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

730 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