XML Video Tutorial 5

XML Entity Video TutorialIn this part of my XML video tutorial, I will cover how to use an entity. I’ll also show you how to combine XML, DTD and XSL files.

You can use an entity to create shortcuts in your DTD files. You create personal codes like those built into XML like &lt; is replaced with <.

Through using these codes you’ll be able to pull data either from within the DTD file, or from external files. All of the code follows the video.

If you like videos like this, tell Google [googleplusone]

Sharing is very much appreciated

Code From the Video


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tvshow SYSTEM "tvshow5.dtd">
<?xml-stylesheet type="text/xsl" href="tvshow5.xsl" ?>
    <name id_code="show_001">Life On Mars</name>
    <network country="US">ABC</network>
    <description>The series tells the story of New York City police detective Sam Tyler. &great; &tv_disclaimer;</description>
			<real_name>Jason O'Mara</real_name>
			<character profession="Detective">Sam Tyler</character>
			<real_name>Michael Imperioli</real_name>
			<character profession="Detective">Ray Carling</character>	
	<poster href="http://ia.media-imdb.com/images/M/MV5BMTM4MDg2NTk1OF5BMl5BanBnXkFtZTcwNzI4OTY5MQ@@._V1._SY317_CR12,0,214,317_.jpg" width="214" height="317" />
    <viewers units="million">7.82</viewers>
    <name id_code="show_002">Life On Mars</name>
    <network country="UK">BBC</network>
    <description>The series combines elements of science fiction and police procedural. &great; &tv_disclaimer;</description>
			<real_name>John Simm</real_name>
			<character profession="Detective">Sam Tyler</character>
			<real_name>Philip Glenister</real_name>
			<character profession="Detective">Gene Hunt</character>	
	<poster href="http://ia.media-imdb.com/images/M/MV5BMTQxNzc5MjQwNF5BMl5BanBnXkFtZTcwMjU3Njc4Mg@@._V1._SY317_CR8,0,214,317_.jpg" width="214" height="317" />
    <viewers units="million">6.8</viewers>


<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT tvshow (show)+>
<!ELEMENT show (name, release, network+, description, actors, poster?, viewers?, (end_date | next_episode))>

<!-- You can use an entity to create shortcuts in your dtd files -->
<!-- An entity just replaces a code like < becomes less than -->
<!-- You can pull the data that will replace the &code; from the dtd
	or from an outside file -->
<!-- There are 5 entities built into xml & &apost; > < " -->
<!-- This is called a parameter entity. It is a shortcut that is
	used inside of a dtd file. It just replaces all occurrences of
	%pd; with (#PCDATA) -->

<!ENTITY % pd "(#PCDATA)">

<!ELEMENT name %pd;> 
<!ATTLIST name id_code ID #REQUIRED>

<!ELEMENT release (#PCDATA)> 

<!ELEMENT network (#PCDATA)> 
<!ATTLIST network country CDATA #REQUIRED>

<!ELEMENT description (#PCDATA)>

<!ELEMENT actors (actor)*>
<!ELEMENT actor (real_name, character)>
<!ELEMENT real_name (#PCDATA)>
<!ELEMENT character (#PCDATA)>
<!ATTLIST character profession CDATA #IMPLIED>

<!-- You can also replace a parameter entity with data 
	from other files -->

<!ELEMENT poster EMPTY> 
<!ATTLIST poster width CDATA #IMPLIED>
<!ATTLIST poster height CDATA #IMPLIED>
<!ATTLIST poster show_id IDREF #IMPLIED>

<!-- %poster; will be replaced with the data in tvposter5.ent -->

<!ENTITY % poster SYSTEM "tvshows5.ent">


<!ELEMENT viewers (#PCDATA)>
<!ATTLIST viewers units (thousand | million) #IMPLIED>

<!ELEMENT end_date (#PCDATA)>
<!ELEMENT next_episode (#PCDATA)>

<!-- You define a general entity in a dtd file that can be used 
	in the xml file -->
<!-- In the xml file type &great; to get the phrase to show up -->

<!ENTITY great "This show was great!">

<!-- In the xml file type &tv_disclaimer; to get the text 
	stored in tvshow5.ent to show up -->

<!ENTITY tv_disclaimer SYSTEM "tvshowsdis.ent">


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" />
	<xsl:template match="/">
			<head><title>TV Shows</title></head>
				<!-- Cycles through each node in the XML file -->
				<xsl:for-each select="tvshow/show">
					<xsl:sort select="name" order="ascending" data-type="text" />
					<p>The show <xsl:value-of select="name"/> was released in
					<xsl:value-of select="release"/> by 
					<xsl:value-of select="network"/>. <xsl:value-of select="description"/></p>
					<p>It had an average 
					viewership of <xsl:value-of select="viewers"/> million people.
					It was cancelled in <xsl:value-of select="end_date"/>
						<!-- Select the attribute to change -->
						<xsl:attribute name="src"> 
						<!-- Set the value for the attribute -->
						<!-- You grab the attribute of a node with @ -->
						<xsl:value-of select="poster/@href" /> 
					</img><br />



All characters appearing in this work are fictitious. 
Any resemblance to real persons, living or dead, is purely coincidental.



<!-- You can assign attributes using one tag like this -->

<!ATTLIST poster 
			height CDATA #IMPLIED
			show_id IDREF #IMPLIED>

13 Responses to “XML Video Tutorial 5”

  1. mr.roshik says:

    hi bro i have no idea about c+ or c++ but a web said me if i learn c++ it will be easy for me to learn any other programming language like php easily, is it true ? suggest me please. and i also have seen that you are very expert in php, python, java, xml , jquery etc. i really want to be a programmer like you.

    • admin says:

      In my opinion C++ would be a terrible first language. You could actually wreck your computer if you did something incorrect with C++.

      I think Python is the best first language, but you’ll eventually be limited by what you can do with Python if you want to program on the web. That still doesn’t mean you shouldn’t learn Python, because it is an easy language to learn and very powerful.

      If you want to create web applications above all else I’d say JavaScript is a great choice. You’ll be able to do great things very fast and it is a pretty easy language to learn.

      After you learn JavaScript, PHP would be an obvious next choice. With PHP and JavaScript you’ll be able to do most anything on the web.

      I hope that helps


  2. Shalini says:

    Thanks Derek for explaining the topic so in detail.

    I have followed the above code step by step but getting the following error when viewed in firefox & IE.

    IE version: 9
    Firefox version: 13.0.1

    XML Parsing Error: undefined entity
    Location: file:///C:/xxx/xxx/xxx/xxx/TVSHOW1.xml
    Line Number 10, Column 170:

    • admin says:

      It can’t locate the TVSHOW1.xml file. You have to point to the location of that file on your computer. It would be different from mine. If you find that everything should work fine.

      • Shalini says:

        Thanks Derek,

        I will check & let you know meanwhile when DTD is embeded inside xml.

        The entities(great) is showing up.

        But when DTD is defined in an external file it is giving error.

        I will let you know

      • Shalini says:

        Thanks Derek,

        When I am placing DTD inside xml file the entity(&great;) is displaying the description.

        but when it is displayed as an external file it is not.

        I will let you once its working.

  3. gabikiwi says:

    1. first of all you have awesome tutorials and thank you very much for sharing.
    2. I think the content of TVSHOW5.ENT and TVSHOWSDIS.ENT should be the other way around.

  4. Anonymous says:

    The naming of the files is incorrect ! Jeee it took me a while 🙂 In the xsl it is correct, the problem is on this page!
    The disclaimer should be in the file tvshowsdis.ent!
    And poster should be in tvshows5.ent. Just change this page and please use filenames in the correct case 🙂
    Do they practice the exercices, the others ? 🙂

  5. Rudy says:

    It is NOT possible to open the XML in Firefox (on Linux/Ubuntu). Did not figure a way to make it work!
    XML Parsing Error: undefined entity
    Location: file:///home/rudy/xml/tutorials/derek/5/tmp/tvshow5.xml
    Line Number 9, Column 90: The series tells the story of New York City police detective Sam Tyler. &great; &tv_disclaimer;

    To open the file in Chromium (Linux/Ubuntu) you have to pass
    rudy@m3800:~$ chromium-browser –allow-file-access-from-files
    ATTENTION: default value of option force_s3tc_enable overridden by environment.


    The Eclipse browser does not complain! Mr Banas, you should try in your Safari to check if you can open the xml. In the video it is from your Eclipse 🙂

  6. Rudy says:

    Yes like it was written by gabikiwi on this page TVSHOW5.ENT and TVSHOWSDIS.ENT reference the wrong contents! Would you be kind to use the naming you make reference in the DTD ? tvshow5.ent and tvshow5dis.ent ? As always thanks a lot for your work Mr Banas!

  7. Rudy says:

    A last remark before to go to the next :
    Eclipse does return the following when the tag description is hovered : The content of element type “description” must match “null”. The tag is underlined in red but that does not prevent the xsl transformation.

Leave a Reply

Your email address will not be published.