Solved

Read a XML file by modified XSL

Posted on 2013-12-25
8
332 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
illegal characters in XML using UTF-8 8 32
c# & linq 7 86
Help with MSXML and Visual Studio 2013 1 38
json format text only 4 65
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now