Solved

Read a XML file by modified XSL

Posted on 2013-12-25
8
335 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
  • 4
  • 3
8 Comments
 
LVL 18

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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing information on Duplicate XML Nodes 24 81
RSS Feed Enclosure URL 1 142
XML Removal- Powershell 4 29
JSON  parse help 7 40
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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