freelanceprogrammers.org Forum Index » XML / XSL
I Finally learned how to take data from a different XML file
Joined: 01 May 2005
Posts: 6
I Finally learned how to take data from a different XML file
It has been a real challenge to learn how the open an XML file, use a
XSLT file to parse it, and
to insert data from a separate XML file, plus sort that data. I am also
using a CSS file to handle
the style issues, for the most part of the HTML document. This allowed
me to go to the
next phase of this self-imposed learning project. The next step was to
do three different sortations:
by title, by subject, by author/s, all of which are accessible by links
on each page. I have done that
now. However, the author/s sortation is not perfect since some books
have multiple authors. This
will require some more effort.
Thank you for any help given!
Here is the code I used to sort by title. I also included the CSS and
data files for any other newbies
that might read this message.
If anyone can tell me how or what to eliminate on this page, please let
me know.
mainPage.xml
<?xml version="1.0" ?><!-- THIS IS THE TITLE SORTATION MAIN PAGE -->
<?xml-stylesheet href="Trans_Titles.xsl" type="text/xsl" ?>
<!-- Some of what follows may not be necessary. -->
<!DOCTYPE html [
<!ENTITY data SYSTEM "./data.xml" >
<!ELEMENT data (item+) >
<!ELEMENT item ANY >
<!ATTLIST item ref ID #IMPLIED >
<!ELEMENT title (#PCDATA) >
<!ELEMENT edition (#PCDATA) >
<!ELEMENT copyright (#PCDATA) >
<!ELEMENT author (#PCDATA) >
<!ELEMENT publisher (#PCDATA) >
<!ELEMENT isbn (#PCDATA) >
<!ELEMENT lccn (#PCDATA) >
<!ELEMENT price ANY >
<!ELEMENT subject ANY >
]>
<!-- This is a reference to the entity created above. -->
<data>&data;</data>
Trans_Titles.xsl
<?xml version="1.0" ?>
<!-- EACH TRANSFORMATION PAGE HAS A SLIGHTLY DIFFERENT APPROACH -->
<!--
This is a fairly simple XSLT page except that it creates a node in the
source
tree from a separate XML document and then it sorts the node and
displays the
results using HTML. The XSLT Sort element, the document( ), and the
call-template are used.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" indent="yes" />
<xsl:template name="SomeStuff" match="/">
<html>
<link rel="stylesheet" type="text/css" href="my_bookstyle.css" />
<head>
<title>MY LIBRARY - SORTED BY TITLE</title>
</head>
<body>
<table>
<tr align="right"><td>BOOKS SORTED BY TITLE</td><td><img
src="./CharlieChan_bw2.jpg" /></td></tr>
<tr><td align="left"><a href="mainSubject.xml">Sort by
Subjects</a></td>
<td align="right"><a href="mainAuthors.xml">Sort by
Authors</a></td></tr>
</table>
<!-- Example of using the document( ) to include a source tree from
a separate XML document -->
<xsl:for-each select="document(`data.xml`) /data/item/."><!-- Notice
the way this code ends -->
<xsl:sort data-type="text" select="normalize-space(title)"
/><!-- NOTICE THE data-type ENTRY -->
<table>
<xsl:call-template name="aTable" /><!-- calling a named
template -->
</table>
<xsl:value-of select="*/*" /><!-- NOTICE HOW THIS WAS
HANDLED -->
</xsl:for-each>
</body>
</html>
</xsl:template>
<!--
I used HTML tags instead of XSLT element tags to display the data in
this table
because it is easier to work with when using a CSS document.
-->
<xsl:template name="aTable">
<tr class="highlightBeige"><td class="aBorderTL"> TITLE </td><td
class="aBorderTR"><xsl:value-of select="title" /></td></tr>
<tr><td>EDITION</td><td><xsl:value-of select="edition" /></td></tr>
<!-- for-each loop -->
<xsl:for-each select="author">
<tr><td>AUTHOR/s</td><td><xsl:value-of select="." /></td></tr>
</xsl:for-each>
<tr><td>PUBLISHER</td><td><xsl:value-of select="publisher" /></td></tr>
<tr><td>ISBN</td><td><xsl:value-of select="isbn" /></td></tr>
<tr><td>LCCN</td><td><xsl:value-of select="lccn" /></td></tr>
<tr><td>PRICE</td><td><xsl:value-of select="price" /></td></tr>
<tr><td class="aBorderBL">SUBJECT</td><td
class="aBorderBR"><p><xsl:value-of select="subject" /></p></td></tr>
<br />
</xsl:template>
</xsl:stylesheet>
my_bookstyle.css
body { font-family: Arial, Helvetica, sans-serif; }
A:link { color: #000000; text-decoration: underline;}
A:visited { color: #336699; text-decoration: underline;}
A:hover { color: #CC9900; text-decoration: underline;}
table { width: 60%; border-style: ridge; border-width: 6px; }
.highlightBlue {background-color:#B9EAFF; }
.highlightBeige {background-color:beige; }
.borderSilver { border-color:#C0C0C0; }
* { font-size: 16px; }
.aBorderBL { border-top-width: 4px; border-right-width: 4px;
border-top-style: dashed; border-right-style: dashed; }
.aBorderBR { border-top-width: 4px; border-top-style: dashed; }
.aBorderTL { border-bottom-width: 4px; border-bottom-style: dashed;
border-right-style: dashed; border-right-width: 4px; }
.aBorderTR { border-bottom-width: 4px; border-bottom-style: dashed; }
.aBorderML { border-bottom-width: 2px; border-bottom-style: solid;
border-right-width: 2px; border-right-style: solid;
border-top-width: 2px; border-top-style: solid; }
.aBorderMR { border-bottom-width: 2px; border-bottom-style: solid;
border-top-width: 2px; border-top-style: solid; }
data.xml
<?xml version="1.0" ?>
<data>
<item ref="0-672-31763-X">
<title>JavaScript Unleashed</title>
<edition>Third Edition</edition>
<copyright>2000</copyright>
<author>Richard Wagner</author>
<author>R. Allen Wyke</author>
<publisher>SAMS</publisher>
<isbn>0-672-31763-X</isbn>
<lccn>99-63993</lccn>
<price>$49.99 USD</price>
<subject>JavaScript Scripting Language - Much of this is about
how to embed JavaScript into HTML:
client-side, server-side, and Microsoft Active Server Pages
(ASP).</subject>
</item>
<item ref="1-884133-56-8">
<title>1001 Visual Basic Programmer`s Tips</title>
<edition>First Edition</edition>
<copyright>1997</copyright>
<author>Kris Jamsa, Ph.D.</author>
<author>Lars Klander</author>
<publisher>Jamsa Press</publisher>
<isbn>1-884133-56-8</isbn>
<lccn></lccn>
<price>$54.95 USD</price>
<subject>Visual Basic 5 Programming Language - It is for the
first time VB Programmer
or the expert. It covers manipulating ActiverX controls, documents,
libraries, and servers. Talks about VBScript.</subject>
</item>
<item ref="0-7821-2523-9">
<title>HTlML 4</title>
<edition>Second Edition</edition>
<copyright>1999</copyright>
<author>Deborah S. Ray</author>
<author>Eric J. Ray</author>
<publisher>SYBEX</publisher>
<isbn>0-7821-2523-9</isbn>
<lccn>99-661301</lccn>
<price>$34.99 USD</price>
<subject>HyperText Markup Language 4 - "... your one-stop
comprehensive guide to HyperText
Markup Language?"</subject>
</item>
<item ref="1-861000-88-X">
<title>Beginning Visual C++ 6</title>
<edition>Third Edition</edition>
<copyright>1998</copyright>
<author>Ivor Horton</author>
<publisher>Wrox Press Ltd.</publisher>
<isbn>1-861000-88-X</isbn>
<lccn></lccn>
<price>$49.99 USD</price>
<subject>Visual C++ 6 - from beginner level to building
an actual, though limited, working Windows program using
Microsoft Foundation Classes. It is a difficult book!</subject>
</item>
<item ref="0-07-222535-1">
<title>Learn to Program with C++</title>
<edition>First Edition</edition>
<copyright>2002</copyright>
<author>John Smiley</author>
<publisher>M cGraw-Hill/Osborne</publisher>
<isbn>0-07-222535-1</isbn>
<lccn></lccn>
<price>$29.99 USD</price>
<subject>C++ Programming Language - This an excellent book for the
true begin to start learning C++.</subject>
</item>
<item ref="0-07-882311-0">
<title>Teach Yourself C</title>
<edition>Third Edition</edition>
<copyright>1997</copyright>
<author>Herbert Schildt</author>
<publisher>Osborne McGraw-Hill</publisher>
<isbn>0-07-882311-0</isbn>
<lccn></lccn>
<price>$27.99 USD</price>
<subject>C Programming Language - "Herb Schildt tells his
programmers what they want and need to
know-- simply, clearly, concisely, and authoritatively". -- ACM
Computer Reviews</subject>
</item>
<item ref="0-7821-2469-0">
<title>Visual Basic 6 COMPLETE</title>
<edition>First Edition</edition>
<copyright>1999</copyright>
<author>Steve Brown</author>
<author>Wayne S. Freeze</author>
<author>Ken Getz</author>
<author>Mike Gilbert</author>
<author>Guy Hart-Davis</author>
<author>Kevin Hough</author>
<author>Susann Novalis</author>
<author>Evangelos Petroutsos</author>
<publisher>SYBEX Inc.</publisher>
<isbn>0-7821-2469-0</isbn>
<lccn>99-61303</lccn>
<price>$19.99 USD</price>
<subject>Visual Basic 6 Programming Language - complete reference,
Visual Basic 6 function reference,
intro to VBA, practical VB 6 examples, debugging info, mastering
the IDE, etc</subject>
</item>
<item ref="0-672-32141-6">
<title>PURE JavaScript</title>
<edition>Second Edition</edition>
<copyright>2002</copyright>
<author>R. Allen Wyke</author>
<author>Jason D. Gilliam</author>
<author>Charlton Ting</author>
<author>Sean Michaels</author>
<publisher>SAMS</publisher>
<isbn>0-672-32141-6</isbn>
<lccn>00-11512</lccn>
<price>$49.99 USD</price>
<subject>JavaScript Scripting Language Reference - A code-intensive
premium reference book</subject>
</item>
<item ref="1-57610-390-0">
<title>Visual Basic 6 Core Language Little Black Book</title>
<edition>First Edition</edition>
<copyright>1999</copyright>
<author>Steven Holzner</author>
<publisher>The Coriolis Group</publisher>
<isbn>1-57610-390-0</isbn>
<lccn></lccn>
<price>$24.99 USD</price>
<subject>Visual Basic 6 Programming Language - "Indispensable
Problem Solver"</subject>
</item>
<item ref="1-56592-631-5">
<title>Win32 API Programming with Visual Basic</title>
<edition>First Edition</edition>
<copyright>2000</copyright>
<author>Steven Roman</author>
<publisher>O`Reilly</publisher>
<isbn>1-56592-631-5</isbn>
<lccn></lccn>
<price>$39.95 USD</price>
<subject>Visual Basic and Win32 APIs - Taking Advantage of Windows
System Services with VB It lists my of the
Windows APIs.</subject>
</item>
</data>
All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help
China Wholesale - Electronics Products
Character Studio - Tutorials and Help







