Bits and Bytes

Learn to Live

XML-Schema Problems with Solutions — March 21, 2011

XML-Schema Problems with Solutions

1.Write an xml schema for the following xml file.

Book.xml

<BOOK>

<BOOK_NAME>Computer Networks</BOOK_NAME>

<AUTHOR>Andrew Tanenbaum</AUTHOR>

<PUBLISHER>Pearson Education</PUBLISHER>

<PUBLISHINGDATE>2010-02-10</PUBLISHINGDATE >

</BOOK>

2. Modify the schema for the above xml so that the BOOK_NAME contains only “Computer Networks” or “Network Programming” or “Internet Programming”.

Also the length of the publisher should not exceed 10 characters.

Solution 1.

Book1.xsd

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;

<xsd:element name=”BOOK”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=”BOOK_NAME” type=”xsd:string”/>

<xsd:element name=”AUTHOR” type=”xsd:string”/>

<xsd:element name=”PUBLISHER” type=”xsd:string”/>

<xsd:element name=”PUBLISHINGDATE” type=”xsd:date”/>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:schema>


Solution2

Book2.xsd

<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;

<xsd:element name=”BOOK”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=”BOOK_NAME” type=”BookNameType” />

<xsd:element name=”AUTHOR” type=”xsd:string” />

<xsd:element name=”PUBLISHER” type=”PublisherLength” />

<xsd:element name=”PUBLISHINGDATE” type=”xsd:date” />

</xsd:sequence>

</xsd:complexType>

</xsd:element>


<xsd:simpleType name=”BookNameType”>

<xsd:restriction base=”xsd:string”>

<xsd:enumeration value=”Computer Networks”></xsd:enumeration>

<xsd:enumeration value=”Network Programming”></xsd:enumeration>

<xsd:enumeration value=”Internet Programming”></xsd:enumeration>

</xsd:restriction>

</xsd:simpleType>


<xsd:simpleType name=”PublisherLength”>

<xsd:restriction base=”xsd:string”>

<xsd:maxLength value=”10″ />

</xsd:restriction>

</xsd:simpleType>


</xsd:schema>


Advertisements
XSL Questions and Solutions in XML — January 29, 2011

XSL Questions and Solutions in XML

Q1.For the below xml, create an xsl which displays only the text “24X7 news” when applied to the below xml.

News.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="News.xsl" ?>
<news>
   <news_item>
        <title>Title</title>
        <content>
           <par>
               This is My News
               <footnote>toi</footnote>
          </par>
        </content>
       <date>19/01/11</date>
        <author>Anki</author>
        <news_agent>TOI</news_agent>
    </news_item>
</news>

Solution

The xsl for the above xml

News.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="news_item">
     <html>
       <h1>24x7 News</h1>
    </html>
</xsl:template>
</xsl:stylesheet>


Open the XML file in a browser. The o/p is shown below

News1

Q2. Modify the xsl, to print the title, date and author of the news item in a tabular format.

Solution

News2.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="news_item">
   <html>
        <br/>
        <table border="1" align="center" cellpadding="10">
         <tr>
           <th>Title</th>
           <th>Date</th>
           <th>Author</th>
         </tr>
         <tr>
             <td><xsl:value-of select="title"/></td>
              <td><xsl:value-of select="date"/></td>
              <td><xsl:value-of select="author"/></td>
         </tr>
      </table>
     </html>
</xsl:template>
</xsl:stylesheet>


To link an XML file with a XSL add this line

Here in News.xml file we added this line

<?xml-stylesheet type=”text/xsl” href=”News2.xsl” ?>

Open this file in a Browser

o/p

News2

Doing XML — December 23, 2010