Solved

Read a XML file by modified XSL

Posted on 2013-12-25
8
333 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

19 Experts available now in Live!

Get 1:1 Help Now