Solved

Read a XML file by modified XSL

Posted on 2013-12-25
8
339 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 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

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. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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