The Wayback Machine - https://web.archive.org/web/20240705101132/https://www.geeksforgeeks.org/displaying-xml-using-css/
Open In App

Displaying XML Using CSS

Last Updated : 09 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

XML stands for Extensible Markup Language. It is a dynamic markup language. It is used to transform data from one form to another form.
An XML file can be displayed using two ways. These are as follows :-

  1. Cascading Style Sheet
  2. Extensible Stylesheet Language Transformation
  3. Displaying XML file using CSS :
    CSS can be used to display the contents of the XML document in a clear and precise manner. It gives the design and style to whole XML document.

    • Basic steps in defining a CSS style sheet for XML :
      For defining the style rules for the XML document, the following things should be done :-

      1. Define the style rules for the text elements such as font-size, color, font-weight, etc.
      2. Define each element either as a block, inline or list element, using the display property of CSS.
      3. Identify the titles and bold them.
    • Linking XML with CSS :
      In order to display the XML file using CSS, link XML file with CSS. Below is the syntax for linking the XML file with CSS:

      <?xml-stylesheet type="text/css" href="name_of_css_file.css"?>
    • Example 1.
      In this example, the XML file is created that contains the information about five books and displaying the XML file using CSS.

      • XML file :
        Creating Books.xml as :-




        <?xml version="1.0" encoding="UTF-8"?>
        <?xml-stylesheet type="text/css" href="Rule.css"?>
        <books>
            <heading>Welcome To GeeksforGeeks </heading>
            <book>
                <title>Title -: Web Programming</title>
                <author>Author -: Chrisbates</author>
                <publisher>Publisher -: Wiley</publisher>
                <edition>Edition -: 3</edition>
                <price> Price -: 300</price>
            </book>
            <book>
                <title>Title -: Internet world-wide-web</title>
                <author>Author -: Ditel</author>
                <publisher>Publisher -: Pearson</publisher>
                <edition>Edition -: 3</edition>
                <price>Price -: 400</price>
            </book>
            <book>
                <title>Title -: Computer Networks</title>
                <author>Author -: Foruouzan</author>
                <publisher>Publisher -: Mc Graw Hill</publisher>
                <edition>Edition -: 5</edition>
                <price>Price -: 700</price>
            </book>
            <book>
                <title>Title -: DBMS Concepts</title>
                <author>Author -: Navath</author>
                <publisher>Publisher -: Oxford</publisher>
                <edition>Edition -: 5</edition>
                <price>Price -: 600</price>
            </book>
            <book>
                <title>Title -: Linux Programming</title>
                <author>Author -: Subhitab Das</author>
                <publisher>Publisher -: Oxford</publisher>
                <edition>Edition -: 8</edition>
                <price>Price -: 300</price>
            </book>
        </books>

        
        

        In the above example, Books.xml is linked with Rule.css which contains the corresponding style sheet rules.

      • CSS FILE :
        Creating Rule.css as:-




        books {
             color: white;
             background-color : gray;
             width: 100%;
        }
         heading {
             color: green;
             font-size : 40px;
             background-color : powderblue;
        }
         heading, title, author, publisher, edition, price {
             display : block;
        }
         title {
             font-size : 25px;
             font-weight : bold;
        }

        
        

      • Output :
      • Example 2.
        In this example, the XML file is created that contains the information about various sections in Geeks for Geeks and the topics they contains and after that displaying the XML file using CSS .

        • XML file :
          Creating Section.xml as :-




          <?xml version="1.0" encoding="UTF-8"?>
          <?xml-stylesheet type="text/css" href="Geeks.css"?>
          <Geeks_for_Geeks>
              <title>Hello Everyone! Welcome to GeeksforGeeks</title>
              <geeks_section>
                  <name>Algo</name>
                  <topic1>Greedy Algo</topic1>
                  <topic2>Randomised Algo</topic2>
                  <topic3>Searching Algo</topic3>
                  <topic4>Sorting Algo</topic4>
              </geeks_section>
              <geeks_section>
                  <name>Data Structures</name>
                  <topic1>Array</topic1>
                  <topic2>Stack</topic2>
                  <topic3>Queue</topic3>
                  <topic4>Linked List</topic4>
              </geeks_section>
              <geeks_section>
                  <name>Web Technology</name>
                  <topic1>HTML</topic1>
                  <topic2>CSS</topic2>
                  <topic3>Java Script</topic3>
                  <topic4>Php</topic4>
              </geeks_section>
              <geeks_section>
                  <name>Languages</name>
                  <topic1>C/C++</topic1>
                  <topic2>Java</topic2>
                  <topic3>Python</topic3>
                  <topic4>Ruby</topic4>
              </geeks_section>
              <geeks_section>
                  <name>DBMS</name>
                  <topic1>Basics</topic1>
                  <topic2>ER Diagram</topic2>
                  <topic3>Normalization</topic3>
                  <topic4>Transaction Concepts</topic4>
              </geeks_section>
          </Geeks_for_Geeks>

          
          

          In the above example, Section.xml is linked with Geeks.css which contains the corresponding style sheet rules.

        • CSS FILE :
          Creating Geeks.css as:-




          Geeks_for_Geeks 
                        {
                        font-size:80%;
                        margin:0.5em;
                        font-family: Verdana;
                        display:block;
                        }
          geeks_section {
                        display:block; 
                        border: 1px solid silver;
                        margin:0.5em; 
                        padding:0.5em; 
                        background-color:whitesmoke;
                        }
          title {
                display:block;
                font-weight:bolder;
                text-align:center;
                font-size:30px;
                background-color: green;
                color: white;
            
                }
          name, topic1, topic2, topic3, topic4 {
                                           display:block;
                                           text-align:center;
                                           }
          name {
               color:green; 
               text-decoration: underline  ;
               font-weight:bolder;
               font-size:20px;
               }
          topic1 {
                 color:green
                 }
          topic2 {
                 color:brown
                 }
          topic3 {
                 color:blue
                 }
          topic4 { 
                 color:orange
                 }

          
          

        • Output :

        Advantages of displaying XML using CSS:

        1. CSS is used in XML or HTML to decorate the pages.
        2. CSS is used for interactive interface, so it is understandable by user.
        3. CSS enable multiple pages to share formatting, and reduce complexity and repetition in the structural content. So page loader is faster.

        Disadvantages of displaying XML using CSS :

        1. Using CSS, no transformation can be applied to the XML documents.
        2. CSS uses different dimensions with different browsers. So the programmer has to run the code in different browser and test its compatibility to post it live.
        3. CSS have different level of versions, so it is confusing for the browser and user.

        CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.



        Previous Article
        Next Article

Similar Reads

Displaying XML Using XSLT
XSLT stands for Extensible Stylesheet Language Transformation. XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspecti
3 min read
Difference Between text/xml and application/xml for Webservice Response
application/xmlThis is the recommended media type for XML data according to the IETF specifications [RFC 7303]. It signifies that the XML content might not be human-readable and could potentially contain processing instructions or other non-human-consumable elements. Syntax:Content-Type: application/xmlThis example demonstrates a web service respon
2 min read
Displaying inline and multiline blocks of code using Bootstrap
Bootstrap provides a number of classes for displaying inline and multiline blocks of code. Displaying Inline Code: Inline code should be wrapped in &lt;code&gt; tags. The resulting text will be displayed in a fixed-width font and given a red font color. Note: The &lt; and &gt; tags should be replaced with &amp;lt; and &amp;gt; respectively. Below i
3 min read
Convert JSON file into CSV file and displaying the data using Node.js
There are so many ways to store data for better understanding for individual purposes, in few cases, JSON files will be more suitable in few cases, CSV files, and there are also lots of other types as well like XML, etc. In this article, we will convert the JSON file data into CSV file data and also display that through Node.js. JSON stands for Jav
3 min read
Which tags are used to displaying data in tabular form in HTML ?
In this article, we will discuss the tags used to display the data in tabular form. Tags: Tags are the starting and ending parts of an HTML element. Every tag starts with the "&lt;" symbol and ends with the"&gt;" symbol. Whatever inside these symbols are called tags. Opening and closing tags are used to store any information inside the tag. Opening
3 min read
How to add CSS in XML File ?
XML stands for Extensible Markup Language. It is a dynamic markup language. It is used to transform data from one form to another form. In this article, we will discuss how to add CSS in the XML file. The CSS can be used to display the contents of the XML document in a clear and precise manner. It gives the design and style to a whole XML document.
2 min read
How to generate an XML file dynamically using PHP?
A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically. The first thing we need to do is fetch the data from the database. For that, we need to w
2 min read
How to parse and process HTML/XML using PHP ?
In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs. The simplexml_load_string() function is used to parse the given XML and then the XML object c
2 min read
How to Convert XML data into JSON using PHP ?
In this article, we are going to see how to convert XML data into JSON format using PHP. Requirements: XAMPP Server Introduction: PHP stands for hypertext preprocessor, which is used to create dynamic web pages. It also parses the XML and JSON data. XML stands for an extensible markup language in which we can define our own data. Structure of XML:
3 min read
Convert xml data into json using Node.js
XML: Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet. It is a textual data format with strong support via Unicode for different human langua
2 min read
three90RightbarBannerImg