Only change we’re doing from previous day tutorial is to: Call the javascript function in the body of html. Display XML data inside DIV tags using innerHTML property of the DOM.
Business card Content index.html
Our Business Card!Satish B(91) 9844552841(91) 9844119841[email protected]Technotip IT SolutionsShwetha(91) 123456789(91) 987654321[email protected]Microsoft
We’re fetching the XML data using getElementById and getElementsByTagName properties and storing them in javascript variables. Later using innerHTML property assigning the value to the DIV tags.
Video Tutorial: Display XML Data With DOM( innerHTML ): XML & Javascript
By displaying the contents on the html page we could facilitate the users to add / remove / modify the XML contents. Going forward, we would show you how to do these using some simple DOM properties.
Output on the browser Name: Satish home: (91) 123456789 work: (91) 987654321 email: [email protected] Company: Microsoft
Video tutorial illustrating the manipulation of XML data using DOM [ Document Object Model ]
Using CSS Level 2, we couldn’t change the order in which we would display / access the XML data. To solve that and many more limitations, we make use of DOM. In this tutorial you can learn, accessing the required XML content and arrange it as you need it.
<html>
<head><title>Our Business Card!</title>
<script language="javascript" type="text/javascript">
function display1() {
var base = document.getElementById("myBiz");
var person1 = base.getElementsByTagName("companyName")[0];
var name = "Name: " + person1.getElementsByTagName("Name")[0].firstChild.data;
var phLable1 = person1.getElementsByTagName("phone")[0].getAttribute("type") + ": ";
var phone1 = phLable1 + person1.getElementsByTagName("phone")[0].firstChild.data;
var phLable2 = person1.getElementsByTagName("phone")[1].getAttribute("type") + ": ";
var phone2 = phLable2 + person1.getElementsByTagName("phone")[1].firstChild.data;
var email = "email: " + person1.getElementsByTagName("email")[0].firstChild.data;
var company;
comanyy = "Company: " + person1.getElementsByTagName("company")[0].firstChild.data;
alert("Business Card: \n\n" + name + "\n" + phone1 + "\n"
+ phone2 + "\n" + email + "\n" + company);
}
function display2() {
var base = document.getElementById("myBiz");
var person2 = base.getElementsByTagName("companyName")[1];
var name = "Name: " + person2.getElementsByTagName("Name")[0].firstChild.data;
var phLable1 = person2.getElementsByTagName("phone")[0].getAttribute("type") + ": ";
var phone1 = phLable1 + person2.getElementsByTagName("phone")[0].firstChild.data;
var phLable2 = person1.getElementsByTagName("phone")[1].getAttribute("type") + ": ";
var phone2 = phLable2 + person2.getElementsByTagName("phone")[1].firstChild.data;
var email = "email: " + person2.getElementsByTagName("email")[0].firstChild.data;
var company;
comapany = "Company: " + person2.getElementsByTagName("company")[0].firstChild.data;
alert("Business Card: \n\n" + name + "\n" + phone1 + "\n"
+ phone2 + "\n" + email + "\n" + company);
}
</script>
</head>
<body>
<xml id="myBiz" style="display: none;">
<companynames>
<companyname>
<name>Satish B</name>
<phone type="home" primary="call me">(91) 9844552841</phone>
<phone type="work">(91) 9844119841</phone>
<email>[email protected]</email>
<company>Technotip IT Solutions</company>
</companyname>
<companyname>
<name>Shwetha</name>
<phone type="home">(91) 123456789</phone>
<phone type="work" primary="call me">(91) 987654321</phone>
<email>[email protected]</email>
<company>Microsoft</company>
</companyname>
</companynames>
</xml>
<a href="javascript:display1()">Satish: Technotip IT Solutions</a><br />
<a href="javascript:display2()">Shwetha: Microsoft</a>
</body>
</html>
<html> <head><title>Our Business Card!</title> <script language="javascript" type="text/javascript"> function display1() { var base = document.getElementById("myBiz"); var person1 = base.getElementsByTagName("companyName")[0]; var name = "Name: " + person1.getElementsByTagName("Name")[0].firstChild.data; var phLable1 = person1.getElementsByTagName("phone")[0].getAttribute("type") + ": "; var phone1 = phLable1 + person1.getElementsByTagName("phone")[0].firstChild.data; var phLable2 = person1.getElementsByTagName("phone")[1].getAttribute("type") + ": "; var phone2 = phLable2 + person1.getElementsByTagName("phone")[1].firstChild.data; var email = "email: " + person1.getElementsByTagName("email")[0].firstChild.data; var company; comanyy = "Company: " + person1.getElementsByTagName("company")[0].firstChild.data; alert("Business Card: \n\n" + name + "\n" + phone1 + "\n" + phone2 + "\n" + email + "\n" + company); } function display2() { var base = document.getElementById("myBiz"); var person2 = base.getElementsByTagName("companyName")[1]; var name = "Name: " + person2.getElementsByTagName("Name")[0].firstChild.data; var phLable1 = person2.getElementsByTagName("phone")[0].getAttribute("type") + ": "; var phone1 = phLable1 + person2.getElementsByTagName("phone")[0].firstChild.data; var phLable2 = person1.getElementsByTagName("phone")[1].getAttribute("type") + ": "; var phone2 = phLable2 + person2.getElementsByTagName("phone")[1].firstChild.data; var email = "email: " + person2.getElementsByTagName("email")[0].firstChild.data; var company; comapany = "Company: " + person2.getElementsByTagName("company")[0].firstChild.data; alert("Business Card: \n\n" + name + "\n" + phone1 + "\n" + phone2 + "\n" + email + "\n" + company); } </script> </head> <body> <xml id="myBiz" style="display: none;"> <companynames> <companyname> <name>Satish B</name> <phone type="home" primary="call me">(91) 9844552841</phone> <phone type="work">(91) 9844119841</phone> <email>[email protected]</email> <company>Technotip IT Solutions</company> </companyname> <companyname> <name>Shwetha</name> <phone type="home">(91) 123456789</phone> <phone type="work" primary="call me">(91) 987654321</phone> <email>[email protected]</email> <company>Microsoft</company> </companyname>
</companynames> </xml> <a href="javascript:display1()">Satish: Technotip IT Solutions</a><br /> <a href="javascript:display2()">Shwetha: Microsoft</a> </body>
</html>
Here we’ve written entire thing in one file, for the purpose of demonstration. We would highly recommend you to write javascript, css in external file and link it to the html file.
Video Tutorial: Read / Access XML Data With DOM: XML & Javascript
First we fetch the root tag and assign it to a javascript variable called base. Using this as a reference, we fetch the sub-element and using which we fetch all its child elements and inturn get the data present inside them.
Remember, the string inside the tags Name, phone email and company is also considered separate node.
Inside the index.html file we have taken two anchor tags: Satish: Technotip IT Solutions Shwetha: Microsoft
which invoke the javascript functions and then display the information of respective people.
If in case we have hundreds or thousands of people in our XML file, we could use looping and dynamically fetch XML data and display it more meaningfully and with less effort(due to proper programmatical automation).
In the previous tutorial there was no facility to display the labels using CSS. To over come that problem, we step into CSS Level 2, wherein we include the label information to the presentation layer using CSS level 2 coding.
Last 3 lines of coding is the only thing changed from previous day tutorial. Those 3 lines are CSS Level 2 coding!
using :beforepseudo class selector we tell the parser to include email: as content before displaying the actual email id.
similarly, using :after we include the label after the actual content.
attr(type) this selects the value assigned to the attribute type from companyNames.xml file and displays it. phone[primary]:after There are multiple phone numbers, one of which is made primary. So phone[primary] points to the phone number which has the attribute primary.
Video Tutorial: Business Card Design: XML & CSS Level 2
I’ve shown CSS Level 2 working on all 3 browsers: Internet Explorer, Mozilla Firefox and Google Chrome. But some older versions of Internet Explorer doesn’t support CSS Level 2 coding. To solve this issue( cross browser compatibility issue ), we can make use of XSLT [ eXtensible Stylesheet Language Transformations ]. We’ll see it in coming video tutorials.
Output on the browser Satish B home: (91) 9844552841(call me) work: (91) 9844119841 email: [email protected] Technotip IT Solutions
In this example we apply styling to business cards of two people. The primary information i.e., the content is in XML and the presentation is taken care by CSS.
Base tag or the root tag name is companyNames and the elements inside root tag is companyName Other tags present inside companyName tag are Name, phone, email, company.
Example: Two people are Satish B and Shwetha, they are working in Technotip IT Solutions and Microsoft respectively.
Video tutorial illustrating: attaching css stylesheet to XML document to give better appearance to it on the browser. In short: for presentation purpose.
To achieve this, we need to first create a stylesheet file, and attach it or associate our XML file with this stylesheet. We do it with the help of a processing instruction.
This way we have separated the content and styling information and made it into two separate files. Helps keep the XML and Design/CSS coding separate and we could hire separate teams to work on these things.
Video Tutorial: Attach CSS StyleSheet To XML Document